比如
<div id="box">
<p>文字</p>
<img src="https://img.uj5u.com/2020/09/16/87590161710441.png"/>
<p>文字</p>
<p>文字</p>
<img src="https://img.uj5u.com/2020/09/16/87590161710441.png"/>
<img src="https://img.uj5u.com/2020/09/16/87590161710441.png"/>
<p>文字</p>
<p>文字</p>
<img src="https://img.uj5u.com/2020/09/16/87590161710441.png"/>
<p>文字</p>
</div>
怎么讓他alert提示:
文字
xxx.png
文字
文字
xxx.png
xxx.png
文字
文字
xxx.png
文字
uj5u.com熱心網友回復:
<html><body>
<div id="box">
<p>文字</p>
<img src="https://img.uj5u.com/2020/09/16/87590161710441.png"/>
<p>文字</p>
<p>文字</p>
<img src="https://img.uj5u.com/2020/09/16/87590161710441.png"/>
<img src="https://img.uj5u.com/2020/09/16/87590161710441.png"/>
<p>文字</p>
<p>文字</p>
<img src="https://img.uj5u.com/2020/09/16/87590161710441.png"/>
<p>文字</p>
</div>
<!--1-1024000-->
<script type="text/javascript">
var boxId = document.getElementById("box");
var iLen = boxId.children.length;
var str ="";
var strTmp ="";
var strImg = "";
for(var i=0; i<iLen; i++)
{
var tag = boxId.children[i].tagName;
if(tag == "P")
{
strTmp+=(boxId.children[i].textContent+"\r\n");
}
else if(tag == "IMG")
{
strTmp += (boxId.children[i].attributes[0].value+"\r\n");
}
}
alert(strTmp);
</script>
</body>
</html>
uj5u.com熱心網友回復:
$("div").text() 得到的是 test,是 div 的純文本,會自動忽略 html 標簽 。$("div").html() 得到的是 <p>test</p>,是 div 的所有內容,包括 html 標簽,它有個好處是可以將 得到的標簽與文字 直接再利用,比如添加到頁面其他地方之類的操作 。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/58560.html
標籤:JavaScript
