我试图查看是否单击了具有.editable类的元素,在这种情况下,我想返回false。正如您在可运行示例中看到的那样,我设法对工作解决方案进行了硬编码。
但是,它仅与editableElements列表中的第一个节点一起使用,这是因为我不知道如何等效于$(editableElements).contains(...),即在if中通过多个节点进行过滤声明。
我将不胜感激。
var elements = document.querySelectorAll('.accordion-item');
Array.prototype.forEach.call(elements, function(el, i) {
// onclick handler
elements[i].onclick = function(e) {
var editableElements = document.querySelectorAll(".editable");
// This line works fine with selecting the first element of editableElements node list:
if (e.target == editableElements[0] || editableElements[0].contains(e.target)) {
// But I want something more like this, containing all elements:
// if (e.target == editableElements || editableElements.contains(e.target))
return false;
}
alert("proceeding as usual");
};
});
<div class="accordion">
<div class="accordion-item">
Click me
</div>
<div class="accordion-item">
Click me
<div class="editable">Click me and nothing happens 1</div>
<div class="editable">Click me and nothing happens 2 <i>(not working because node element 1 is hard-coded)</i></div>
</div>
</div>