【发布时间】:2010-09-18 13:44:17
【问题描述】:
我的困境相当简单:这个函数根据<ul> 的父id 获取'this' <li> 的id 元素。它过去可以正常工作,但现在不行了,我要么需要<ul> 使用classes 而不是id,同时仍然能够将“当前”的id 分配给当前元素,或者更改我的
function myFunction(element) {
liArray = document.getElementById("leftlist").childNodes;
i = 0;
while (liArray[i]) {
liArray[i].id = "";
i++;
}
element.id = "current"; // or element.className ?
}
ul#leftlist {
background-color: rgb(205, 205, 205);
}
ul#leftlist li#current a {
color: rgb(96, 176, 255);
background-color: 218, 218, 218);
}
ul#leftlist li a {
color: rgb(86, 86, 86);
}
#leftlist a:link {
color: rgb(86, 86, 86);
background-color: #ddd;
}
#leftlist a:active {
color: rgb(96, 176, 255);
background-color: rgb(218, 218, 218);
}
<ul id="leftlist">
<li onClick='myFunction(this);'>
<a href="123" bla bla </a></li>
<li onClick='myFunction(this);'> .... etc.</li>
</ul>
也许我需要更改我的 CSS。这以前有效,但现在当前的id 无效,因为即使我通过 JavaScript 分配id="current",ul#leftlist li a 也具有优先权。
【问题讨论】:
标签: javascript html css