简体   繁体   English

通过类名称获取祖先

[英]Get Ancestor by class name

I have a component which have buttons and list both of which perform events on click. 我有一个具有按钮的组件,并列出了两个按钮均在单击时执行事件。 I need a common way to get the ancestor element for these elements. 我需要一种通用方法来获取这些元素的祖先元素。 The structure looks like 结构看起来像

<div class='a'>
   <button class ='b' data-name="hello">
      <span class ='c'>clickMe
         <span>somehting</span>
      </span>
   <button>
   <ul class ='d'>
      <li data-name="about">
          <span class ='e'>something here
             <span>somehting</span>
          </span>
      </li>
      <li data-name="home">
          <span class ='e'>something elser here
              <span>somehting</span>
          </span>
      </li>
   </ul>
</div>

I try to get the element button and li because I need to get data-name information. 我尝试获取元素按钮和li,因为我需要获取数据名称信息。 e.target is the element that was clicked e.target是被单击的元素

var targetel = goog.dom.getAncestorByClass(e.target,null,class??);

Not sure how to get the correct element irrespective if its a button or li. 不确定如何获取正确的元素,无论它是按钮还是li。 Do i need to add a unique class to all the elements ? 我是否需要为所有元素添加一个唯一的类?

Just use e.currentTarget 只需使用e.currentTarget

 var result = document.querySelector('#result'); var clickables = document.querySelectorAll('button, li'); //add click listener to elements for (var i = 0, l = clickables.length; i < l; i++) { clickables[i].addEventListener('click', getDataName); } function getDataName(e) { var dataName = e.currentTarget.getAttribute('data-name'); result.textContent = dataName; } 
 <div class='a'> <button class ='b' data-name="hello"> <span class ='c'>clickMe <span>somehting</span> </span> </button> <ul class ='d'> <li data-name="about"> <span class ='e'>something here <span>somehting</span> </span> </li> <li data-name="home"> <span class ='e'>something elser here <span>somehting</span> </span> </li> </ul> </div> <div id="result">data-name of clicked element goes here</div> 

https://api.jquery.com/parents/ https://api.jquery.com/parents/

Use the jquery.parents("li") function. 使用jquery.parents(“ li”)函数。 it will select all the parents that match your css filter. 它将选择与您的CSS过滤器匹配的所有父级。 so you can do 所以你可以做

var parentli = targete1.parents("li");

var button = targete1.parents("div").children[0];

Or something similar to that. 或类似的东西。

EDIT: Not sure why i got downvoted, here is my idea in action. 编辑:不知道为什么我被拒绝,这是我的想法在行动。

maybe press f12 to inspect element and look at the console log. 也许按f12键检查元素并查看控制台日志。

 var onClick = function () { var parentEl = $(this).parents("button, li")[0]; console.log(parentEl); $("#result")[0].innerText = parentEl.getAttribute("data-name"); }; $('span.c').click(onClick); $('li span.e').click(onClick); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='a'> <button class='b' data-name="hello"> <span class='c'>clickMe <span>something</span> </span> </button> <ul class ='d'> <li data-name="about"> <span class ='e'>something here <span>somehting</span> </span> </li> <li data-name="home"> <span class ='e'>something elser here <span>something</span> </span> </li> </ul> </div> <div id="result">result goes here</div> 

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

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