我有带有产品目录的页面,需要使用它的名称(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/