简体   繁体   English

遍历子级并获取属性

[英]Loop through child and get attribute

I'm trying to create a div which should replace the select element (because the real one is not necessary in that case) and I try to get the data-select attribute of each option (which represented by an a tag) but I probably do something wrong and I'm getting null . 我正在尝试创建一个应该替换select元素的div(因为在那种情况下不需要真正的那个),并且我尝试获取每个optiondata-select属性(由a标签表示),但是我可能做错事我会null that's my code - HTML: 那是我的代码-HTML:

<div class="select" data-after="&#x43;">
    <i data-select-value="">Date &amp; Time</i>
    <div class="options">
        <a href="#" data-select="Date &amp; Time">Date &amp; Time</a>
        <a href="#" data-select="Alphabetical">Alphabetical</a>
        <a href="#" data-select="Modified">Modified</a>
    </div>
</div>
<div class="select" data-after="&#x43;">
    <i data-select-value="">Delete</i>
    <div class="options">
        <a href="#" data-select="Delete">Delete</a>
        <a href="#" data-select="Edit">Edit</a>
        <a href="#" data-select="Unpublish">Unpublish</a>
    </div>
</div>

JS: JS:

var selectbox = document.getElementsByClassName('select');
for(var i = 0; i < selectbox.length; i++) {
    selectbox[i].onclick = function() {
        var elechild = this.childNodes;
        var x = 0;
        for(x; x < elechild.length; x++) {
            elechild[x].onclick = function() {
                console.log(this.getAttribute('data-select'));
            }
        }
    }
}

How can I solve this and get each attribute (please, no jQuery)? 如何解决此问题并获取每个属性(请不要使用jQuery)? (btw please excuse my english if I had any misspellings) (顺便说一句,如果我有拼写错误,请原谅我的英语)

Thank you very much! 非常感谢你!

Basically, you can get all elements using querySelectorAll in javascript. 基本上,您可以使用javascript中的querySelectorAll获取所有元素。 https://developer.mozilla.org/de/docs/Web/API/Document/querySelectorAll https://developer.mozilla.org/de/docs/Web/API/Document/querySelectorAll

Try this: 尝试这个:

 var selectors = document.querySelectorAll('.options a'); for (var x = 0; x < selectors.length; x++) { selectors[x].onclick = function() { console.log(this.getAttribute('data-select')); } } 
 <div class="select" data-after="&#x43;"> <i data-select-value="">Date &amp; Time</i> <div class="options"> <a href="#" data-select="Date &amp; Time">Date &amp; Time</a> <a href="#" data-select="Alphabetical">Alphabetical</a> <a href="#" data-select="Modified">Modified</a> </div> </div> <div class="select" data-after="&#x43;"> <i data-select-value="">Delete</i> <div class="options"> <a href="#" data-select="Delete">Delete</a> <a href="#" data-select="Edit">Edit</a> <a href="#" data-select="Unpublish">Unpublish</a> </div> </div> 

You really had overthought that a bit. 您确实有点想不到。 You only need to gather up the links and loop through them. 您只需要收集链接并遍历它们。 You might have noticed that in your code, you had to click twice for anything to happen - - that was because you were applying a click event to ALL the children of the select div elements as well as the select div elements, which included the option div. 您可能已经注意到,在你的代码,你必须点击两次发生什么事- -那是因为你的应用点击事件给所有的孩子都select div元素还有select div元素,其中包括option div。

Also, it's better to not use the onXyz event properties of DOM objects and instead use the W3C DOM Event standard , which uses addEventListener as an event callback registration method. 另外,最好不要使用DOM对象的onXyz事件属性,而应使用W3C DOM Event标准 ,该标准使用addEventListener作为事件回调注册方法。 This allows for a more standard and more robust event registration / de-registration model. 这允许一个更标准,更强大的事件注册/注销模型。

 // Just get the anchors that are children of your options element: var selectAnchors = document.querySelectorAll(".options a"); // Just loop through the anchors. No need to loop the divs for(var x = 0; x < selectAnchors.length; x++) { selectAnchors[x].addEventListener("click", function() { console.log(this.getAttribute('data-select')); }); } 
 <div class="select" data-after="&#x43;"> <i data-select-value="">Date &amp; Time</i> <div class="options"> <a href="#" data-select="Date &amp; Time">Date &amp; Time</a> <a href="#" data-select="Alphabetical">Alphabetical</a> <a href="#" data-select="Modified">Modified</a> </div> </div> <div class="select" data-after="&#x43;"> <i data-select-value="">Delete</i> <div class="options"> <a href="#" data-select="Delete">Delete</a> <a href="#" data-select="Edit">Edit</a> <a href="#" data-select="Unpublish">Unpublish</a> </div> </div> 

Use this: 用这个:

var options = document.querySelectorAll('.select .options a');
for (var x = 0; x < options.length; x++) {
  options[x].onclick = function() {
    console.log(this.getAttribute('data-select'));
  }
}

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

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