2012-10-13 27 views
1

免得说,这是我的div:的JavaScript DIV删除链接,如果HREF存在

<div id="success"> 
<a href="/index.php">link1</a> 
<a href="/index.php">!AnotherLink!</a> 
<a href="cat.php">link3</a> 
</div> 

我想移去那些所谓的成功DIV任何链接是相同的“href”

它应该是这样的:

<div id="success"> 
<a href="/index.php">link1</a> 
<a href="cat.php">link3</a> 
</div> 

回答

2

这应该工作:

function removeDups() { 
    var container = document.getElementById("success"); 
    var a = container.getElementsByTagName("a"); 
    var hrefs = {}; 
    for (var i = 0; i < a.length; i++) { 
     if (a[i].href in hrefs) { 
      a[i].parentNode.removeChild(a[i]); 
     } else { 
      hrefs[a[i].href] = 1; 
     } 
    } 
} 

http://jsfiddle.net/fDPsH/

+0

谢谢:)作品。 – Nmmsda