简体   繁体   English

为什么在.html()之后没有触发click事件? (jQuery的)

[英]Why is the click event is not fired after .html() ? (jQuery)

Please, consider this code: 请考虑以下代码:

<script>
$(function(){
   $('#clickme').click(function(){   
      $('#note_pag').html('<div id="note_pag"><ul><li><b>1</b></li><li><a href="2">2</a></li></ul></div>');
   }); 

   $('#note_pag a').click(function(){           
     alert($(this).attr('href'));                  
    return false;
   });
});
</script>


<a href="#" id="clickme">CLICK ME</a>

<div id="note_pag">
  <ul>
    <li><a href="1">1</a></li>
    <li><a href="2">2</a></li>
    <li><a href="3">3</a></li>
    <li><a href="4">4</a></li>
  </ul>
</div>

Could someone tell me why I can get the clicks on the links inside #node_app , but when I replace the div with .html() the event is not fired anymore? 有人可以告诉我为什么我可以获得#node_app内部链接的点击,但是当我用.html()替换div时,事件不再被触发了?

Because your essentially using .bind (via .click ) the events are only set for the current elements. 因为你基本上使用.bind (通过.click )事件只是为当前元素设置。 You're then removing those elements. 然后,您将删除这些元素。 If you want those events to persist after you've altered the dom, use .live instead: 如果您希望在更改dom后这些事件仍然存在,请使用.live代替:

$("#note_page a").live("click", function() {
    alert($(this).attr("href"));
    return false;
});

You need live - 你需要 -

$('#note_pag a').live('click',function(){           
    alert($(this).attr('href'));                  
    return false;
});

also, you should avoid creating a duplicate id when inserting the new content. 此外,您应该避免在插入新内容时创建重复ID。 You could chnage note_page to a class instead of an id. 您可以将note_page更改为类而不是id。

Binding to events via click() or bind() only works for objects that are already present in the DOM at the time of binding your event handler. 通过click()bind()绑定到事件仅适用于绑定事件处理程序时DOM中已存在的对象。

What you are looking for is jQuery's live() function, which allows you to bind handlers to not-yet-existing elements. 您正在寻找的是jQuery的live()函数,它允许您将处理程序绑定到尚未存在的元素。

Your code would look like this: 您的代码如下所示:

$('#note_pag a').live('click', function(){
    alert($(this).attr('href'));                  
    return false;
});

Edit: To be precise, live() binds to $(document) and checks the target attribute of events that have bubbled all the way up through the DOM – that's why the binding works even though elements are added after binding the event callback. 编辑:确切地说, live()绑定到$(document)并检查通过DOM一直冒泡的事件的目标属性 - 这就是绑定工作的原因,即使绑定事件回调后添加了元素。

Example HTML: 示例HTML:

<div id="some_triggers">
    <a id="foo" href="#">Foo!</a>
</div>

Example jQuery: 示例jQuery:

$('#foo').click(function(){ alert('foo') });
$('#bar').click(function(){ alert('bar') });
$('#some_triggers').append('<a id="bar" href="#">Bar!</a>');
$('#foo').live('click', function(){ alert('foo live()!') });
$('#bar').live('click', function(){ alert('bar live()!') });

// -> clicking #foo alerts "foo" and "foo live()"
// -> clicking #bar alerts "bar live()" only, since the '.click()' binding doesn't work on non-existent DOM elements.

Edit2: EDIT2:

As mentioned by some of the others (@Marnix, most notably), your "replacement" code is nesting elements with the same ID inside each other every time the click handler executes. 正如其他一些人所提到的那样(@Marnix,最值得注意的是),每次点击处理程序执行时,您的“替换”代码都会在彼此内嵌相同ID的元素。

You should replace your 你应该更换你的

 $('#note_pag').html('<div id="note_pag"><ul><li><b>1</b></li><li><a href="2">2</a></li></ul></div>');

line with either 与任何一条相符

$('#note_pag').replaceWith('<div id="note_pag"><ul><li><b>1</b></li><li><a href="2">2</a></li></ul></div>');

or 要么

 $('#note_pag').html('<ul><li><b>1</b></li><li><a href="2">2</a></li></ul>');
$('#note_pag a')

This says "find all a elements within #note_pag ". 这是说“找到所有a中的元素#note_pag ”。 You then bind an event handler to those elements. 然后,将事件处理程序绑定到这些元素。

$('#note_pag').html('<div id="note_pag"><ul><li><b>1</b></li><li><a href="2">2</a></li></ul></div>');

When you do this, the elements that the event handlers were bound to above are replaced. 执行此操作时,将替换事件处理程序绑定到上面的元素。 (Actually, you're also adding <div id="note_pag"> every time: you don't need to include the html of the element you're filling.) (实际上,你每次都添加<div id="note_pag"> :你不需要包含你正在填充的元素的html。)

Effectively, you need to bind the event handler to a different element, one that always exists. 实际上,您需要将事件处理程序绑定到另一个始终存在的元素。 This is possible because ancestor elements are notified of events on descendant elements. 这是可能的,因为祖先元素会被通知后代元素上的事件。 The nicest way to do this with jQuery is the delegate method: 使用jQuery执行此操作的最好方法是delegate方法:

$('#note_pag').delegate('a', 'click', function() {
    alert($(this).attr('href'));                  
    return false;
});

Note that this does the same thing as live , but is significantly better optimised in various ways. 请注意,这与live相同,但是以各种方式显着优化。

event isn't set to the new html after the click 单击后事件未设置为新的html

try: 尝试:

$('#note_pag a').live('click', function(){  

尝试使用.live()

Instead of $('#note_pag a').click you would need to use $('#note_pag a').live("click",handler) . 而不是$('#note_pag a').click你需要使用$('#note_pag a').live("click",handler) Reason : The event was attached to the a node and when you overrite the HTML it becomes a new a node 原因:事件已附加到节点,当您覆盖HTML时,它将成为新的节点

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

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