jQuery的.not()的香草javascript等效项是什么?

编程入门 行业动态 更新时间:2024-10-24 13:31:57
本文介绍了jQuery的.not()的香草javascript等效项是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试创建一个将类添加到列表的循环.我希望将此类添加到除单击元素之外的所有其他元素上.我在互联网上找到了以下内容:

I'm trying to create a loop which adds a class to a list. I wish this class to be added on all the elements except the element which is being clicked. I found the following on the internet:

$('.elements').click(function(){ $('.elements').not(this).each(function(){ // do something }); })

有与香草javascript等效的东西吗?我找不到来自 .not()的转换.

Is there an equivalent to vanilla javascript? I cannot find the conversion from .not().

推荐答案

您发布的完整代码的原始版本将是这样.相当详细的TBH,而无需帮助程序向每个el添加点击侦听器.

The vanilla version of the full code you posted would be something like this. Quite verbose TBH without having the helper to add a click listener to each el.

var els = document.querySelectorAll('.elements'); [].forEach.call(els, function(el, i, els) { el.addEventListener('click', function() { [].forEach.call(els, function(el) { if(el !== this) { // do something } }, this); }); });

更多推荐

jQuery的.not()的香草javascript等效项是什么?

本文发布于:2023-10-07 15:30:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469782.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:香草   jQuery   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!