[英]javaScript HTML Parsing
我正在嘗試解析HTML文檔。
這是HTML:
<div>
<a class="profilePicThumb" href="https://www.facebook.com/photo.php?fbid=669986173135523&set=a.219741158160029.56045.100003724408511&type=3&source=11" rel="theater">
<img class="profilePic img" alt="Zinedine Zidane" src="https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xtp1/v/t1.0-1/p160x160/11817031_669986173135523_8947137909221952926_n.jpg?oh=e21bb7d0d03858df94987c62e64a6022&oe=56904830&__gda__=1455998716_5401014ec3ef47b8bffd62207c57fda0" />
</a>
</div>
我需要得到圖片和名字。
我嘗試以下代碼:
var list = document.getElementsByClassName("profilePic img");
有人可以幫助我嗎? 我是javaScript的新手...
謝謝
您的代碼似乎可以正常工作,並且可以正確找到HTMLImageElment,請參見下文:
var list = document.getElementsByClassName("profilePic img"); document.write("<b>Number of element:</b>" + list.length + '<br>'); document.write("<b>alt: </b>" + list[0].alt + '<br>') document.write("<b>src: </b>" + list[0].src + '<br>')
<div> <a class="profilePicThumb" href="https://www.facebook.com/photo.php?fbid=669986173135523&set=a.219741158160029.56045.100003724408511&type=3&source=11" rel="theater"> <img class="profilePic img" alt="Zinedine Zidane" src="https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xtp1/v/t1.0-1/p160x160/11817031_669986173135523_8947137909221952926_n.jpg?oh=e21bb7d0d03858df94987c62e64a6022&oe=56904830&__gda__=1455998716_5401014ec3ef47b8bffd62207c57fda0" /> </a> </div>
您的list
分配正常。 只需使用:
if list.length > 0 {
var name = list[0].alt;
var url = list[0].src;
// whatever you want to do with name and url
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.