开发者

jquery html disappears on click event

开发者 https://www.devze.com 2023-02-05 18:55 出处:网络
Please see the following code: <body> &开发者_如何学Golt;form id=\"form1\" runat=\"server\">

Please see the following code:

<body>
&开发者_如何学Golt;form id="form1" runat="server">
<div>

    <button id="helloButton">
        Search</button>
    <div id="hello">


    </div>
    <script type="text/javascript">
        $(document).ready(
        function () {

            $('#helloButton').click(function () {


                $('#hello').html('<div>hello world</div>');

            });

        });
    </script>
</div>
</form>

When I use this script, all it does is flash "hello world", it doesn't keep it on the page. Does this has something to do with the click event? I'm trying to click the button and then leave the object on the page.


Otherwise you can simply do a return false which will do both preventDefault and stop propagation

$('#helloButton').click(function (e) {

    $('#hello').html('<div>hello world</div>');
    return false;
});


in order to prevent the form from posting, so that you can see your changes, you need to call preventDefault()

$('#helloButton').click(function (e) {
    e.preventDefault();
    $('#hello').html('<div>hello world</div>');
});
0

精彩评论

暂无评论...
验证码 换一张
取 消