【问题标题】:How to get div's selector using this如何使用此获取 div 的选择器
【发布时间】:2019-09-03 09:38:46
【问题描述】:

所以我想通过 JQuery 使用 $(this) 来获取 div 的选择器 这个怎么做?会是这样吗? $("...这个")

<div id="divId">
     <span onclick="myFunction()">Some Text</span>
     <span onclick="myFunction()">Some Text</span>
     <span onclick="myFunction()">Some Text</span>
     <span onclick="myFunction()">Some Text</span>
</div>
<script>
myFunction(){
selector im asking for.doSomething();
}

</script>

【问题讨论】:

  • 没有“ 选择器”这样的东西,任何 HTML 元素都可以使用无数种不同的方式进行选择。你真的是说你想得到divId的价值吗? (如果是这样,那是为了什么?)

标签: javascript selector


【解决方案1】:

如果你想获取父母的id:

myFunction(){
  $(this).parent('div').attr('id');
}

如果您需要获取 div 本身,请删除 .attr('id')

myFunction(){
   $(this).parent('div');
}

【讨论】:

    【解决方案2】:

    由于您有 div 的 id,您可以简单地使用 $('#divId') 来选择 div。

    【讨论】:

      【解决方案3】:

      试试类似的东西

      $('#divId').click(function(){
      $(this).hide();
      })

      【讨论】:

        猜你喜欢
        • 2011-12-24
        • 2010-10-24
        • 2021-12-23
        • 1970-01-01
        • 2018-01-15
        • 1970-01-01
        • 2022-12-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多