首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在单击时切换已安装的元素

在单击时切换已安装的元素
EN

Stack Overflow用户
提问于 2018-01-12 01:09:38
回答 1查看 31关注 0票数 0

代码语言:javascript
复制
$( 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();
    });
});
代码语言:javascript
复制
<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()返回未定义的。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-12 01:16:45

rmtrail是一个类,因此请尝试.rmtrail

代码语言:javascript
复制
$( 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();
    });
 });
代码语言:javascript
复制
<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>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48212468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档