简体   繁体   English

如何通过ai关键字识别元素?

[英]How identify element by ai keyword?

In the following code, there is a way to identify the "li class=""...> who has a "a id=""...>" tag with a specific ai="2711766". How can i identify them by "ai"? If were "id" i would use "getElementById" but in this case, identify by "ai", i don't know how to do it! 在下面的代码中,有一种方法可以识别带有特定ai =“ 2711766”标签的“ li class =”“ ...>谁具有” a id =“” ...>“标记。我该如何识别如果是“ id”,我会使用“ getElementById”,但是在这种情况下,要使用“ ai”来标识,我不知道该怎么做!

...
<li class="" style="background-color: rgb(246, 237, 245);"> … </li>
<li class="" style="background-color: rgb(246, 237, 245);"> … </li>
<li class="" style="background-color: rgb(246, 237, 245);">
    <a id="a_32447396" class="aVideo" ai="2711766" i="há 4 horas">
        <div class="imgVideo" onmouseover="startThumbSlide('32447396', '0')">
            <div class="photo2">
                <div id="f_32447396" class="lazyVideos"></div>
            </div>
        </div>
        <h3 style="color: rgb(133, 6, 123);"> … </h3>
    </a>
    <p> … </p>
</li>
<li class=""> … </li>
<li class="" style="background-color: rgb(246, 237, 245);"> … </li>
<li class="" style="background-color: rgb(246, 237, 245);"> … </li>
...

Thank you! 谢谢!

try using data attriubte. 尝试使用data属性。

  <a id="a_32447396" class="aVideo" data-ai="2711766" data-i="há 4 horas">

You can reach the values with dataset like this: 您可以使用以下dataset达到值:

var el = document.getElementById('a_32447396')
console.log(el.dataset.ai); //2711766

The ai is an attribute. ai是一个属性。 Knowing this you could get the value searching the element by tag name using getElementsByTagName instead by id and then get the attribute like this: 知道这一点,您可以使用getElementsByTagName而不是id通过标签名称搜索元素的值,然后获取如下属性:

var element = document.getElementsByTagName("a")[0]; // in this example the first link is taken!
alert(element.getAttribute("ai"));

Output is: 输出为:

2711766

The drawback of this is, that you will get all elements of the given type (fe links - a) and you need to iterate over them. 这样做的缺点是,您将获得给定类型的所有元素(fe链接-a),并且需要对其进行迭代。

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

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