繁体   English   中英

jQuery HTML在单击事件中消失

[英]jquery html disappears on click event

请参见以下代码:

<body>
<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>

当我使用此脚本时,它所做的只是flash“ hello world”,它不会保留在页面上。 这与点击事件有关吗? 我试图单击按钮,然后将对象留在页面上。

为了防止表单发布,以便您可以看到更改,您需要调用preventDefault()

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

否则,您可以简单地执行return false,这将同时防止preventDefault和停止传播

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM