jquery html在点击事件上消失

loc*_*boy 5 jquery

请看下面的代码:

<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>
Run Code Online (Sandbox Code Playgroud)

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

kob*_*obe 4

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

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

    $('#hello').html('<div>hello world</div>');
    return false;
});
Run Code Online (Sandbox Code Playgroud)

  • return 应该在函数的末尾吗? (2认同)
  • @cfram54,感谢编辑,我忘了......我直到今天都在度假,并且与编码失去了联系......我正在热身 (2认同)