简体   繁体   English

如何从getElementsByClassName的DOM对象中获取信息

[英]How do I get information from a DOM object from getElementsByClassName

I am currently modifying a website, I need to get the current slide's class name, and modify the img tag after run 我当前正在修改一个网站,我需要获取当前幻灯片的类名,并在运行后修改img标签

var test=document.getElementsByClassName("activeslide");

it give the value of test 它给出了测试的价值

[<li class="slide-4 activeslide" style="visibility: visible; opacity:1;">         ]
    <a target="_blank">
        <img src="img/floorplans/suite-A.jpg" style="height: 560px; width: 495px; left:132px; top:0px">
    </a>
</li>

how do I get the slide-4 and how do I edit the img tag by DOM? 如何获取slide-4,以及如何通过DOM编辑img标签? I tried to do test.innerHTML, and tried to convert test into string, test.toString(); 我尝试做test.innerHTML,并尝试将测试转换为字符串test.toString();。 but it returns me "[object HTMLCollection]" 但它返回我“ [object HTMLCollection]”

You can use myElement.className or myElement.getAttribute("class") where myElement is a reference to your element. 您可以使用myElement.classNamemyElement.getAttribute("class") ,其中myElement是对元素的引用。 Newer browsers also have classList . 较新的浏览器也具有classList

getElementsByClassName() returns an array. getElementsByClassName()返回一个数组。 Following code will give you the img DOM element: 以下代码将为您提供img DOM元素:

var liArray = document.getElementsByClassName("activeslide");
var imgArray = liArray[0].getElementsByTagName("img");
var img = imgArray[0];

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

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