<div class="MyName">
majedur
</div>
我希望majedur使用MyName
答案 0 :(得分:2)
document.getElementsByClassName(&#34; MyName&#34;)将返回具有类&#34; MyName&#34;的元素集合,您可以通过它索引索引
var elements= document.getElementsByClassName("MyName");
if(elements.length > 0){
var value = elements[0].innerHTML;
}
答案 1 :(得分:1)
试试这个
var divText = document.getElementsByClassName('MyName')[0].innerHTML;
alert(divText);//for alert the inner text of the div
console.log(divText);//write in console the inner text of the div