document.querySelectorAll根据元素属性值选择元素

需求

需要选择所有拥有 οnmοuseοver=“top.ShowTip_YC(evt)” 属性的 text 元素,即根据dom元素上的事件名来查找dom并隐藏

const elements = document.querySelectorAll('text[οnmοuseοver="top.ShowTip_YC(evt)"]');
elements.forEach(element => {
    
    
  element.style.display = 'none';
});

使用document.querySelectorAll选择器来选择指定的 HTML 元素,然后对其应用样式或操作。
在这个选择器中,我们使用了属性选择器 [attribute=value],其中 attribute 是 HTML 元素的属性名称,value 是属性值。所以这个选择器的意思是选择所有拥有 οnmοuseοver=“top.ShowTip_YC(evt)” 属性的 text 元素。

jquery的写法

$('text[οnmοuseοver="top.ShowTip_YC(evt)"]').hide()

猜你喜欢

转载自blog.csdn.net/qq_42611074/article/details/130484059
今日推荐