切换插入的元素onclick

问题描述 投票:0回答:1

$( 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更新?我一直在网上看,找不到它。

javascript jquery dom insert
1个回答
1
投票

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>
© www.soinside.com 2019 - 2024. All rights reserved.