Javascript 使用ajax覆盖内容时,jquery单击事件不起作用

Javascript 使用ajax覆盖内容时,jquery单击事件不起作用,javascript,jquery,Javascript,Jquery,我已经在下面的代码中解释了这个问题 <div id='content'> <div id='help'> blah blah blah once there lived a king named midas blah blah blah </div> <script> $(document).ready(function() { $('#help').click( function () { $('help').hi

我已经在下面的代码中解释了这个问题

<div id='content'>

<div id='help'>
blah blah blah
once there lived a king named midas
blah blah blah
</div>

<script>
$(document).ready(function() {
    $('#help').click( function () {
          $('help').hide(500);
     })
})
</script>

<!-- bottom of the page after a long other content -->
</div>
<!-- end of div id= content -->

<script>
function ondelete()
{
// doing an ajax request to after deleting some items to dynamically update a list.
// the result will also have the same above div code with that help div
   document.getElementById('content').innerHTML = xmlHTTP.responseText
}
</script>
当用户单击同一个help按钮时,或者如果用户在help div内单击,则我执行以下代码来隐藏该div$(“#help”).hide(500)

在我发出ajax请求以更新div id='content'的内容之前,这一切都很正常 我正在用相同的内容覆盖相同的帮助div。这就像我在重写 除页眉和页脚外的整个内容区域

但是在用响应文本更新它之后,document.ready中代码顶部的click事件不起作用,因为onclick正在处理该div

我的意思是它工作得很好。 如果单击时没有这一点,并且使用上面的文档就绪代码,当从ajax重写时,它将无法工作

根据我的经验,当使用ajax动态获取和更新带有脚本src标记和脚本标记的代码时,javascript代码或链接不起作用

现在我使用onclick而不是准备隐藏和显示help div的文档

最重要的是

when there is a overwrite from ajax if you again set
$(#'help').click = .... code
this works.

like 
function ondelete()
{
   document.getElementById('content').innerHTML = xmlHTTP.responseText
   $('#help').click (function() { $('#help').hide(500); });
}
因此,在使用ajax进行更新时,似乎必须更新所有附加的事件

请提出您的意见和建议以克服此问题。

尝试使用

.live(事件类型、处理程序)

从1.9版开始,.live()已被弃用

然后你可以使用

将处理程序附加到所有事件的事件 与当前值匹配的元素 选择器,现在或将来

尝试:

而不是:

$('#help').click(function() { 
    // ...
});

Live已被弃用。您应该在上使用

$(文档)。在(“单击”,“选择器”,函数(){

是上的实时/委托格式


现在它真的如预期的那样工作:

$(document).on("click","#id_of_anything_div_input_button_etc", function () {
old_body_of_functon_called_right_here(); });

function old_body_of_functon_called_right_here() { /* ... */}
请注意,自JQuery 1.9以来,live()已被弃用,您必须将live()重写为on()-JQuery甚至不再知道该函数。该函数将如下所示:
$(文档)。on(“单击”,“帮助”,函数(){…});
$('#help').live('click', function() { 
    // ...
});
$('#help').click(function() { 
    // ...
});
$(document).on("click","#id_of_anything_div_input_button_etc", function () {
old_body_of_functon_called_right_here(); });

function old_body_of_functon_called_right_here() { /* ... */}