简体   繁体   English

在onclick上切换插入的元素

[英]Toggle an insterted element onclick

 $( document ).ready(function() { $(".exmore").before("<span class='rmtrail'>...</span>"); $(document).on("click",".exmore", function(e){ console.log("clicked"); console.log($(this).siblings("rmtrail").html()); $(this).siblings("rmtrail").toggle(); }); }); 
 <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> </head> <body> <a href='#' class='exmore'>open</a> </body> </html> 

I am trying to toggle the inserted .rmtrail element when the .exmore element is clicked but the $(this).siblings(); 我试图在单击.exmore元素但$(this).siblings();时切换插入的.rmtrail元素$(this).siblings(); method is not working. 方法不起作用。 console.log() returns undefined. console.log()返回未定义。

Is there a way to force the jQuery DOM to update after an element is inserted? 有没有一种方法可以在插入元素后强制jQuery DOM更新? I have been looking online and can not find it. 我一直在网上寻找,找不到。

rmtrail is a class so try .rmtrail rmtrail是一类,所以尝试.rmtrail

 $( document ).ready(function() { $(".exmore").before("<span class='rmtrail'>...</span>"); $(document).on("click",".exmore", function(e){ console.log("clicked"); console.log($(this).siblings(".rmtrail").html()); $(this).siblings(".rmtrail").toggle(); }); }); 
 <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> </head> <body> <a href='#' class='exmore'>open</a> </body> </html> 

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

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