简体   繁体   English

查找 html 元素,没有正确的 class 名称或 ID,带有 javascript

[英]find html element, without proper class name or id, with javascript

I have this HTML code我有这个 HTML 代码

<div class="option-wrapper">
    <label class="">
        <input type="checkbox" name="name" value="1" data-type-xml="select" aria-label="Untitled">
        <span lang="" class="option-label active">1</span>
    </label>
    <label class="">
        <input type="checkbox" name="name" value="2" data-type-xml="select" aria-label="Untitled">
        <span lang="" class="option-label active">2</span>
    </label>
</div>

im trying to find the labels and to set a "style="display: none;" like this <label class="" style="display: none;>我试图找到标签并设置“style="display: none;" 像这样<label class="" style="display: none;>

my problem is, I only need one of the labels in this div.我的问题是,我只需要这个 div 中的一个标签。 But I don't have a class name or id to find one label.但我没有 class 名称或 ID 来查找 label。 Do u guys know a solution for this?你们知道解决方案吗?

Use document.getElementsByTagName('label') instead for all end then get them with the array offset.使用document.getElementsByTagName('label')代替所有结束,然后使用数组偏移量获取它们。 Or - to select the first - use document.getElementByTagName('label') .或者 - 到 select 第一个 - 使用document.getElementByTagName('label') Notice the singular version of the second selector.注意第二个选择器的单数版本。

Or, a more general selector which you can use with either classes or tags or any other query string would be document.querySelector('label') or document.querySelectorAll('label')或者,可以与类或标签或任何其他查询字符串一起使用的更通用的选择器是document.querySelector('label')document.querySelectorAll('label')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM