0

我有带有产品目录的页面,需要使用它的名称(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()仅影响第一个“类别块”div。如何对页面上的所有此类 div 使用此功能?

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

4

1 回答 1

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>
于 2013-03-20T16:18:31.123 回答