【问题标题】:How can I filter different Tag name of DOM element using javascript如何使用 javascript 过滤 DOM 元素的不同标签名称
【发布时间】:2021-05-09 14:37:25
【问题描述】:

在这里,我使用 querySelectorAll 在数组中插入了所有必需的标记名,我想有条件地对不同的元素执行不同的操作。如何在遍历每个元素时获取不同的标签名称,

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
  }
})

【问题讨论】:

    标签: javascript html node.js reactjs


    【解决方案1】:

    在 forEach 循环中,您可以在迭代的当前元素上使用 tagName 属性并对其进行比较。参考MDN:https://developer.mozilla.org/en-US/docs/Web/API/Element/tagName

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-06
      • 2020-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多