[英]How can I filter different Tag name of DOM element using javascript
Here, I've inserted all required tagname inside an array using querySelectorAll, I want to perform different action on different elements conditionally.在这里,我使用 querySelectorAll 在数组中插入了所有必需的标记名,我想有条件地对不同的元素执行不同的操作。 How can I get the different tag name while iterating through each element,如何在遍历每个元素时获取不同的标签名称,
const allCanvas = document.getElementsByTagName("canvas");
const allSvg = document.querySelectorAll(
".widget-graph-container svg.rsm-svg "
);
const allElements = [...allCanvas, ...allSvg];
here I have different canvas and svg elements inside allElements array, I want to iterate through each element inside array and perform different action.
allElements.forEach((el, index) => {
if (el == 'canvas') {
// do something
} else if (el == 'svg') {
// do something
}
})
In the forEach loop, you could use the tagName property on the current element of the iteration and put a comparison on that.在 forEach 循环中,您可以在迭代的当前元素上使用tagName属性并对其进行比较。 Refer to the MDN: https://developer.mozilla.org/en-US/docs/Web/API/Element/tagName参考 MDN: https://developer.mozilla.org/en-US/docs/Web/API/Element/tagName
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.