首先,抱歉英语不好。
好吧,我有3个段落,每个段落都有一些链接,我必须按段落显示链接
继承人我的HTML& JS代码:
<head>
<script type="text/javascript">
function pEn(){
var nump=document.getElementsByTagName("p");
var nEn=new Array();
for (var i = 0; i < nump.length; i++) {
nEn=document.getElementsByTagName("a");
for (var j = 0; j < nEn.length; j++) {
alert("Parrafo numero "+(i+1)+": "+nEn[j]);
};
};
}
</script>
</head>
<body>
<p>
<a href="http://www.google.es">First link</a>
<a href="http://www.stackoverflow.com">Second link</a>
</p>
<p>
<a href="http://www.neoteo.com">Third link</a>
</p>
<p>
<a href="http://fp.edu.gva.es/">Fourth link</a>
</p>
<div>
<button onclick="pEn()">Links by paragraph</button>
</div>
</body>
我想得到的是: P1:www.fdsjkfls.com,P1:www.fjkdslfjsklo.com,P2:www.sdklf.com,P3:www.vnsdwo.com
不是这样的: P1:www.fdsjkfls.com,P1:www.fjkdslfjsklo.com,P1:www.sdklf.com,P1:www.vnsdwo.com,P2:www.fdsjkfls.com,P2:www.fjkdslfjsklo.com,P2: www.sdklf.com,P2:www.vnsdwo.com,...
任何sugestions?
答案 0 :(得分:2)
选择每个段落中的链接:
for (var i = 0; i < nump.length; i++) {
nEn=nump[i].getElementsByTagName("a"); // <-- Here use nump[i] instead of document
for (var j = 0; j < nEn.length; j++) {
alert("Parrafo numero "+(i+1)+": "+nEn[j]);
};
};