切换插入的元素onclick

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了切换插入的元素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>

当点击.rmtrail元素但是.exmore方法不起作用时,我试图切换插入的$(this).siblings();元素。 console.log()返回undefined。

有没有办法在插入元素后强制jQuery DOM更新?我一直在网上看,找不到它。

答案

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>

以上是关于切换插入的元素onclick的主要内容,如果未能解决你的问题,请参考以下文章