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

时间:2023-01-25 07:26:47

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? I tried to do test.innerHTML, and tried to convert test into string, test.toString(); but it returns me "[object HTMLCollection]"

我如何获得slide-4以及如何通过DOM编辑img标签?我试着做test.innerHTML,并尝试将test转换为string,test.toString();但它返回给我“[对象HTMLCollection]”

2 个解决方案

#1


1  

You can use myElement.className or myElement.getAttribute("class") where myElement is a reference to your element. Newer browsers also have classList.

您可以使用myElement.className或myElement.getAttribute(“class”),其中myElement是对元素的引用。较新的浏览器也有classList。

#2


0  

getElementsByClassName() returns an array. Following code will give you the img DOM element:

getElementsByClassName()返回一个数组。以下代码将为您提供img DOM元素:

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

#1


1  

You can use myElement.className or myElement.getAttribute("class") where myElement is a reference to your element. Newer browsers also have classList.

您可以使用myElement.className或myElement.getAttribute(“class”),其中myElement是对元素的引用。较新的浏览器也有classList。

#2


0  

getElementsByClassName() returns an array. Following code will give you the img DOM element:

getElementsByClassName()返回一个数组。以下代码将为您提供img DOM元素:

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