从childNodes的innerHTML动态获取链接

时间:2013-03-20 15:31:10

标签: dynamic parent-child dynamic-data innerhtml

我有产品目录的页面,需要使用它的名称(innerHTML)为每个产品分配链接。该页面如下:

<!DOCTYPE html>
<html>
<head>
<script>
function searchlinks() {
var catbox=document.getElementById("category-box");
var boxcont=catbox.getElementsByTagName("a");
var h3href=catbox.getElementsByTagName("h3");

var www0="../search/" + h3href[0].innerHTML + "+" + boxcont[0].innerHTML;
var www1="../search/" + h3href[0].innerHTML + "+" + boxcont[1].innerHTML;
var www2="../search/" + h3href[0].innerHTML + "+" + boxcont[2].innerHTML;
var www3="../search/" + h3href[0].innerHTML + "+" + boxcont[3].innerHTML;

boxcont[0].href=www0;
boxcont[1].href=www1;
boxcont[2].href=www2;
boxcont[3].href=www3;

}
</script>
</head>
<body onload="searchlinks()">

<div id="category-box"><div class="category-block"><div class="category-blockimage"><img src="" alt="" ></div><div class="category-blockcontent">
     <h3>Clothing</h3>
<a href="">Men's</a>
<a href="">Women's</a>
<a href="">Boys'</a>
<a href="">Girls'</a>
</div></div></div>

<div id="category-box"><div class="category-block"><div class="category-blockimage"><img src="" alt="" ></div><div class="category-blockcontent">
     <h3>Protective Gear</h3>
<a href="">Chin Straps</a>
<a href="">Facemasks</a>
<a href="">Flak Jackets</a>
<a href="">Girdles</a>
<a href="">Hand Pads</a>
<a href="">Arm Pads</a>
<a href="">Helmets</a>
<a href="">Hip Pads</a>
</div></div></div>
</body>
</html>

但我遇到了很多问题。以下是我的问题:
1)如何动态分配链接?(不使用var = www0,www1 ......)
2)函数searchlinks()仅影响第一个&#34;类别块&#34; DIV。 如何在页面上的所有此类div上使用此功能?

这是我的jsfiddle:http://jsfiddle.net/RGe8U/1/

1 个答案:

答案 0 :(得分:1)

这是固定脚本和html。我为两个不同的div给出了不同的id,因为id应该是唯一的,然后单独搜索它们并为它们调用addLink函数。

<html>
<head>
<script>
function addLink(catbox) {
        var boxcont=catbox.getElementsByTagName("a");
        var h3href=catbox.getElementsByTagName("h3");
        //alert(h3href[0].innerHTML);
        for (var i=0; i<boxcont.length; i++) {
                boxcont[i].href="../search/" + h3href[0].innerHTML + "+" + boxcont[i].innerHTML;
        }
}
function searchlinks() {
        addLink (document.getElementById("category-box"));
        addLink (document.getElementById("category-box2"));
}
</script>
</head>
<body onload="searchlinks()">

<div id="category-box"><div class="category-block"><div class="category-blockimage"><img src="" alt="" ></div><div class="category-blockcontent">
     <h3>Clothing</h3>
<a href="">Men's</a>
<a href="">Women's</a>
<a href="">Boys'</a>
<a href="">Girls'</a>
</div></div></div>

<div id="category-box2"><div class="category-block"><div class="category-blockimage"><img src="" alt="" ></div><div class="category-blockcontent">
     <h3>ProtectiveGear</h3>
<a href="">Chin Straps</a>
<a href="">Facemasks</a>
<a href="">Flak Jackets</a>
<a href="">Girdles</a>
<a href="">Hand Pads</a>
<a href="">Arm Pads</a>
<a href="">Helmets</a>
<a href="">Hip Pads</a>
</div></div></div>
</body>
</html>

编辑:如果您不想更改div ID,请使用以下脚本:

    <script>
function addLink(catbox) {
        var boxcont=catbox.getElementsByTagName("a");
        var h3href=catbox.getElementsByTagName("h3");
        alert(h3href[0].innerHTML);
        for (var i=0; i<boxcont.length; i++) {
                boxcont[i].href="../search/" + h3href[0].innerHTML + "+" + boxcont[i].innerHTML;
        }
}
function searchlinks() {
        var divs = document.getElementsByTagName("div");
        for (var i=0; i<divs.length; i++) {
                if (divs[i].id === "category-box")
                addLink (divs[i]);
        }
}
</script>