document.getElementsByClassName – 如何获取只具有指定类的元素 – 没有次要或子类

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

document.getElementsByClassName - How to get elements having ONLY the specified Class - no Secondary or Subclasses

问题

使用getElementsByClassName,我找不到一种方法可以仅查找具有“Cover”类的元素,而不查找还具有“Cover”类和“place-holder”类的元素。

另外,我对“place-holder”类的第二个类应该使用什么术语有疑问。我们是否应该将该类称为子类,还是仅仅将其视为元素上的第二个类?

以下是简单的JavaScript代码:

document.getElementsByClassName("Cover");

// 找到五个元素。如何告诉它仅查找具有Cover类的元素?

请注意,以上内容是你要翻译的部分,不包括问题部分。

英文:

How do I locate ONLY the elements having the class "Cover" but not the elements having more classes than Cover. Using getElementsByClassName I was not able to discover a way for it to NOT also find elements having the "Cover" class and also the "place-holder" class.

Also I question what the correct terminology is for the 2nd class of "place-holder". Do we refer to that class as a subclass or is it simply just a 2nd class on the element?

<div>
	<a class="Cover" href="javascript:void(0);" tabindex="1" aria-label="First Name: firstName" style="height: 38px; left: 26px; width: 1035px; top: 274.375px;"></a>
	<a class="Cover" href="javascript:void(0);" tabindex="2" aria-label="Last Name: lastName" style="height: 38px; left: 26px; width: 1035px; top: 314.375px;"></a>
	<a class="Cover" href="javascript:void(0);" tabindex="3" aria-label="Suffix: Suffix" style="height: 38px; left: 26px; width: 1035px; top: 354.375px;"></a>
	<a class="Cover place-holder" href="javascript:void(0);" tabindex="10" style="height: 34px; left: 26px; width: 1035px; top: 634.375px;"></a>
	<a class="Cover" href="javascript:void(0);" tabindex="20" aria-label="Gender: Gender" style="height: 108px; left: 26px; width: 1035px; top: 1619.75px;"></a>
</div>

Simple Javascript:

document.getElementsByClassName("Cover");

// finds five elements.  How do I tell it to find ONLY elements having the Cover Class?

答案1

得分: 2

let elems = [...document.getElementsByClassName('Cover')]
  .filter(i => i.classList.length === 1)

console.log(elems)
<div>
    <a class="Cover" href="javascript:void(0);" tabindex="1" aria-label="名字:firstName" style="height: 38px; left: 26px; width: 1035px; top: 274.375px;"></a>
    <a class="Cover" href="javascript:void(0);" tabindex="2" aria-label="姓氏:lastName" style="height: 38px; left: 26px; width: 1035px; top: 314.375px;"></a>
    <a class="Cover" href="javascript:void(0);" tabindex="3" aria-label="后缀:Suffix" style="height: 38px; left: 26px; width: 1035px; top: 354.375px;"></a>
    <a class="Cover place-holder" href="javascript:void(0);" tabindex="10" style="height: 34px; left: 26px; width: 1035px; top: 634.375px;"></a>
    <a class="Cover" href="javascript:void(0);" tabindex="20" aria-label="性别:Gender" style="height: 108px; left: 26px; width: 1035px; top: 1619.75px;"></a>
</div>
英文:

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

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

let elems = [...document.getElementsByClassName(&#39;Cover&#39;)]
  .filter(i=&gt;i.classList.length===1)
  
console.log(elems)

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

&lt;div&gt;
	&lt;a class=&quot;Cover&quot; href=&quot;javascript:void(0);&quot; tabindex=&quot;1&quot; aria-label=&quot;First Name: firstName&quot; style=&quot;height: 38px; left: 26px; width: 1035px; top: 274.375px;&quot;&gt;&lt;/a&gt;
	&lt;a class=&quot;Cover&quot; href=&quot;javascript:void(0);&quot; tabindex=&quot;2&quot; aria-label=&quot;Last Name: lastName&quot; style=&quot;height: 38px; left: 26px; width: 1035px; top: 314.375px;&quot;&gt;&lt;/a&gt;
	&lt;a class=&quot;Cover&quot; href=&quot;javascript:void(0);&quot; tabindex=&quot;3&quot; aria-label=&quot;Suffix: Suffix&quot; style=&quot;height: 38px; left: 26px; width: 1035px; top: 354.375px;&quot;&gt;&lt;/a&gt;
	&lt;a class=&quot;Cover place-holder&quot; href=&quot;javascript:void(0);&quot; tabindex=&quot;10&quot; style=&quot;height: 34px; left: 26px; width: 1035px; top: 634.375px;&quot;&gt;&lt;/a&gt;
	&lt;a class=&quot;Cover&quot; href=&quot;javascript:void(0);&quot; tabindex=&quot;20&quot; aria-label=&quot;Gender: Gender&quot; style=&quot;height: 108px; left: 26px; width: 1035px; top: 1619.75px;&quot;&gt;&lt;/a&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 2

简单地:

<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-js -->
const onlyCover = document.querySelectorAll('.Cover');

onlyCover.forEach(elm => {
  console.log(elm.className, elm.textContent);
})

<!-- 语言: lang-html -->
<div>
  <a href="#" class="Cover">aa</a>
  <a href="#" class="Cover">bb</a>
  <a href="#" class="Cover">cc</a>
  <a href="#" class="Cover place-holder">dd</a>
  <a href="#" class="Cover">ee</a>
</div>

<!-- 结束代码片段 -->
英文:

simply:

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

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

const onlyCover = document.querySelectorAll(&#39;[class=&quot;Cover&quot;]&#39;);

onlyCover.forEach( elm =&gt;
  {
  console.log( elm.className, elm.textContent );
  })

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

&lt;div&gt;
  &lt;a href=&quot;#&quot; class=&quot;Cover&quot; &gt;aa&lt;/a&gt;
  &lt;a href=&quot;#&quot; class=&quot;Cover&quot; &gt;bb&lt;/a&gt;
  &lt;a href=&quot;#&quot; class=&quot;Cover&quot; &gt;cc&lt;/a&gt;
  &lt;a href=&quot;#&quot; class=&quot;Cover place-holder&quot; &gt;dd&lt;/a&gt;
  &lt;a href=&quot;#&quot; class=&quot;Cover&quot; &gt;ee&lt;/a&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月18日 08:45:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490445.html
匿名

发表评论

匿名网友

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

确定