有没有在原生JS中在DOM元素集合上循环而不使用循环的方法?

huangapple go评论49阅读模式
英文:

Is there any way to loop over collection of DOM elements without looping in vanilla JS?

问题

我想操作一组DOM元素。

这是一个示例:

HTML:

<div class="items">
  <p class="item">这里有一些文本</p>
  <p class="item">这里有另一些文本</p>
  <p class="item">这里有一些更多的文本</p>
</div>

JS:

const items = document.querySelectorAll(".item")

// 这是我以纯JS方式实现的方式
items.forEach(item => {
  item.style.color = "#333"
})

// 我想要实现类似jQuery的效果,不必使用循环

$(".item").css("color", "#333")

提前感谢!

我尝试寻找解决方案,但找不到最佳解决方案。

英文:

I want to manipulate a collection of DOM elements..

Here's an Example:

HTML:

&lt;div class=&quot;items&quot;&gt;
  &lt;p class=&quot;item&quot;&gt;Here&#39;s some text&lt;/p&gt;
  &lt;p class=&quot;item&quot;&gt;Here&#39;s another text&lt;/p&gt;
  &lt;p class=&quot;item&quot;&gt;Here&#39;s some another text&lt;/p&gt;
&lt;/div&gt;

JS:

const items = document.querySelectorAll(&quot;.item&quot;)

// This is how I do this vanilla JS way
items.forEach(item =&gt; {
  item.style.color = &quot;#333&quot;
})

// I want to achieve something like jQuery where we don&#39;t have to use loop

$(&quot;.item&quot;).css(&quot;color&quot;, &quot;#333&quot;)

Thanks in advance!

I tried finding solutions but could not find the best solution.

答案1

得分: 4

以下是翻译好的部分:

这是一个不使用循环的版本,而且速度非常快 - 我改变了颜色以展示它的效果更好:

const someStyle = `<style>div.items .item { color: #A33; }</style>`;

document.head.insertAdjacentHTML('beforeend', someStyle);
<div class="items">
  <p class="item">这里有一些文本</p>
  <p class="item">这里有另一些文本</p>
  <p class="item">这里有一些其他文本</p>
</div>
英文:

Here is one version that does not use a loop and it is really fast too - I changed the colour to show it works better:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const someStyle = `&lt;style&gt;div.items .item { color: #A33; }&lt;/style&gt;`;

document.head.insertAdjacentHTML(&#39;beforeend&#39;, someStyle);

<!-- language: lang-html -->

&lt;div class=&quot;items&quot;&gt;
  &lt;p class=&quot;item&quot;&gt;Here&#39;s some text&lt;/p&gt;
  &lt;p class=&quot;item&quot;&gt;Here&#39;s another text&lt;/p&gt;
  &lt;p class=&quot;item&quot;&gt;Here&#39;s some another text&lt;/p&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 3

你可以在NodeList原型上进行Monkey Patch,或者编写一个类似的函数/类/工厂,而无需进行Monkey Patch(例如jQuery的方式):

NodeList.prototype.css = function(name, val){
  this.forEach(elem => elem.style[name] = val);
  return this;
};

class DomSet{
  constructor(nodelist){
    this.nodelist = nodelist;
  }
  css(name, val){
    this.nodelist.forEach(elem => elem.style[name] = val);
    return this;    
  }
}

const $ = function(selector){
  return new DomSet(document.querySelectorAll(selector));
}

document.querySelectorAll('.items .item')
  .css('color', 'red')
  .css('background', 'lightgray');
  
$('.items2 .item')
  .css('color', 'blue')
  .css('background', 'yellow');
<div class="items">
  <p class="item">Here's some text</p>
  <p class="item">Here's another text</p>
  <p class="item">Here's some another text</p>
</div>

<div class="items2">
  <p class="item">Here's some text</p>
  <p class="item">Here's another text</p>
  <p class="item">Here's some another text</p>
</div>
英文:

You can monkey patch the NodeList prototype or write an alike function/class/factory without monkey patching (like jQuery for example):

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

NodeList.prototype.css = function(name, val){
  this.forEach(elem =&gt; elem.style[name] = val);
  return this;
};

class DomSet{
  constructor(nodelist){
    this.nodelist = nodelist;
  }
  css(name, val){
    this.nodelist.forEach(elem =&gt; elem.style[name] = val);
    return this;    
  }
}

const $ = function(selector){
  return new DomSet(document.querySelectorAll(selector));
}

document.querySelectorAll(&#39;.items .item&#39;)
  .css(&#39;color&#39;, &#39;red&#39;)
  .css(&#39;background&#39;, &#39;lightgray&#39;);
  
$(&#39;.items2 .item&#39;)
  .css(&#39;color&#39;, &#39;blue&#39;)
  .css(&#39;background&#39;, &#39;yellow&#39;);

<!-- language: lang-html -->

&lt;div class=&quot;items&quot;&gt;
  &lt;p class=&quot;item&quot;&gt;Here&#39;s some text&lt;/p&gt;
  &lt;p class=&quot;item&quot;&gt;Here&#39;s another text&lt;/p&gt;
  &lt;p class=&quot;item&quot;&gt;Here&#39;s some another text&lt;/p&gt;
&lt;/div&gt;

&lt;div class=&quot;items2&quot;&gt;
  &lt;p class=&quot;item&quot;&gt;Here&#39;s some text&lt;/p&gt;
  &lt;p class=&quot;item&quot;&gt;Here&#39;s another text&lt;/p&gt;
  &lt;p class=&quot;item&quot;&gt;Here&#39;s some another text&lt;/p&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月6日 20:01:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定