简体   繁体   English

DOM操作后事件处理程序不起作用

[英]Event Handlers not working after DOM manipulation

On page load I bind Event Handlers with content which is hidden on at the time of page load. 在页面加载时,我将事件处理程序与在页面加载时隐藏的内容绑定。

If users clicks on the button, the hidden content is pulled and replaces the main content of the page, Now the event Handlers which were initially binded do not work. 如果用户单击按钮,则隐藏的内容将被拉出并替换页面的主要内容。现在,最初绑定的事件处理程序不起作用。

HTML code on Page load 页面加载的HTML代码

<p> Initial content of the page </p>
<button id="button"> Click Here to change content</button>
<div class="show-later" style="display: none;"> Some Hidden content </div>  

After the user clicks a button the new dom looks some thing like this 用户单击按钮后,新的dom看起来像这样

<p>
 <div>Some Hidden content</div>
</p>

After the manipulation the event handlers binded to the div element do not work any more. 操作之后,绑定到div元素的事件处理程序将不再起作用。 Please notice that the div goes into the P element after DOM Manipulation. 请注意,在DOM操作之后,div进入了P元素。

jQuery Code: jQuery代码:

   $('#button').click(function(){
      var show_later = $('.show-later').html();
      $('p').html(show_later);
   });

   $(document).ready(function(){
      $('.show-later').click(function(){
       // Do something.....
      });
   });

You're not moving the <div> , you're just taking its content (a text node) and copying that to the paragraph. 您没有移动<div> ,只是获取其内容(文本节点)并将其复制到段落中。 Contrary to what you've stated in the question, the resulting DOM will actually look like this: 与您在问题中所说的相反,生成的DOM实际上看起来像这样:

<p> Some Hidden content </p>
<button id="button"> Click Here to change content</button>
<div class="show-later" style="display: none;"> Some Hidden content </div>

The <div> is still there, it still has content, and it still has the event handler for click bound to it; <div>仍然存在,它仍然具有内容,并且还具有绑定到click的事件处理程序; but it's also still hidden, so there's no way you can click on it. 但它仍然处于隐藏状态,因此无法单击它。

I'd suggest that you actually move the <div> into the <p> element, like so: 我建议您实际上将<div>移到<p>元素中,如下所示:

$('.show-later').show().appendTo('p');

That will select the <div> , make it visible, and then move the element itself into the <p> . 这将选择<div> ,使其可见,然后将元素本身移至<p>

If you do 如果你这样做

var show_later = $('.show-later').html();
$('p').html(show_later);

then no event handler bound to .show-later will become bound to the p . 那么绑定到.show-later事件处理程序将不会绑定到p All you're doing is changing the contents of the p . 您要做的就是更改p的内容。 I suggest changing the HTML as such: 我建议这样更改HTML:

<p class="hide-later"> Initial content of the page </p>
<p class="show-later" style="display: none;"> Some Hidden content </div>
<button id="button"> Click Here to change content</button>

and the javascript as such: 和JavaScript一样:

$('.show-later').show();
$('.hide-later').hide();

have you tried to change your code like this? 您是否尝试过像这样更改代码?

$(document).ready(function(){
    $(document).on('click', '.show-later', function(){
        // Do something.....
    });
});

Update2 : Update2

$('#button').click(function(){
    var show_later = $('.show-later').html();
    $('p').html(show_later).addClass('show-later');
});

Update1 : I'm not sure, what your problem exactly is. Update1 :我不确定,您的问题到底是什么。 Maybe you want to make a jsfiddle-example... 也许您想制作一个jsfiddle-example ...

The 'on'-Method registers events so that even if you remove the element a make a new one, the events is already registered. “ on”方法注册事件,因此即使您删除一个新元素,该事件也已注册。

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

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