最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do i access childNode of <label> using document.getElementsByClassName()? - Stack Overflow

programmeradmin2浏览0评论
<html>
<head></head>
<body>
<span class="mtb-price">
<label Class="mtb-ofr"><b class="lb1"></b>AAAAA</label></span>
<script>
var sku = document.getElementsByClassName("mtb-ofr").childNodes[1].nodeValue;
alert(sku);
</script>
</body>
</html>

How do i access childNode of having class name 'mtb-ofr' using document.getElementsByClassName() ? and what should be the alternative of document.getElementsByClassName() to obtain the same result ??

<html>
<head></head>
<body>
<span class="mtb-price">
<label Class="mtb-ofr"><b class="lb1"></b>AAAAA</label></span>
<script>
var sku = document.getElementsByClassName("mtb-ofr").childNodes[1].nodeValue;
alert(sku);
</script>
</body>
</html>

How do i access childNode of having class name 'mtb-ofr' using document.getElementsByClassName() ? and what should be the alternative of document.getElementsByClassName() to obtain the same result ??

Share Improve this question asked Jun 22, 2012 at 18:41 Pooja DesaiPooja Desai 2271 gold badge6 silver badges16 bronze badges 3
  • 2 document.getElementsByClassName("mtb-ofr")[0].childNodes ? or just .children. – sachleen Commented Jun 22, 2012 at 18:45
  • 2 getElementsByClassName returns an array, you need to select an item of the array before you can drill down any deeper – jackwanders Commented Jun 22, 2012 at 18:45
  • Basically what i want is to replace AAAAA with some other text. – Pooja Desai Commented Jun 22, 2012 at 18:48
Add a ment  | 

3 Answers 3

Reset to default 2

to change the label's text:

document.getElementsByClassName("mtb-ofr")[0].childNodes[1].nodeValue = 'something';

getElementsByClassName returns an array. You need to first get the element from the NodeList.

document.getElementsByClassName("mtb-ofr")[0].childNodes[1].nodeValue

Use jQuery !

$('label.mtb-ofr').children()

You're done !

And you can filter more by passing parameters to children().

http://api.jquery./children/

发布评论

评论列表(0)

  1. 暂无评论