admin管理员组文章数量:1417070
My predicament is fairly simple: This function gets the id
of 'this' <li>
element based on parent id
of <ul>
. It used to work fine but not any more, I will either need to have <ul>
use class
es instead of id
while still being able to assign id
of 'current' to the current element, or change my css.
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>
My predicament is fairly simple: This function gets the id
of 'this' <li>
element based on parent id
of <ul>
. It used to work fine but not any more, I will either need to have <ul>
use class
es instead of id
while still being able to assign id
of 'current' to the current element, or change my css.
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>
Perhaps I need to change my css. This worked before but now the current id
is not being effective as ul#leftlist li a
takes priority even when i assign id="current"
via JavaScript.
5 Answers
Reset to default 2You should use classes for things like "current", not the id. The id normally shouldn't change, and "current" isn't a very good id, as you could have a bunch of things on a page that is "current" in some way. If you need some css style to override another, you can force it with !important:
ul#leftlist li.current a {
color: rgb(96,176,255) !important;
background-color:218,218,218) !important;
}
I'd suggest you using the jQuery framework. It will provide a nice abstraction over your DOM and make it way easier for you to find objects in your DOM and assign events to those objects.
Try reading this tutorial over at jQuery.. I am sure it will amaze you :)
It looks like you do need to change one line in your css:
ul#leftlist li#current a{color: rgb(96,176,255); background-color:218,218,218);}
It's missing the rgb(
after background-color
and should look like:
ul#leftlist li#current a{color: rgb(96,176,255); background-color: rgb(218,218,218);}
I'd also agree with Erlend, current
would be better suited to a class.
Do you do your change in a way, so that you end up with more than one element having the same id ?-)
That is clearly against the whole idea, using id's they have to be totally unique within the context of the html-document ...
-- the first thing, you ought to do is to be sure you only address your li-elements:
liArray=element.parentNode.getElementsByTagName("li");
Secondly it could be an idea, to ensure higher priority for one of your selectors by setting something more in the selector:
Highest priority is gained, if you use an inline style-attribute on the tag, but this doesn't seem to be of any use here.
IDs give a high priority, class give a fair priority and element- and pseudo-selectors give a low priority ...
However each type of priority is accumulated and in that way you can give one selector very high priority by preceding it with another of same priority-type:
ul#leftlist
gives 1 point in high priority and 1 point in low priority
#myParent ul#leftlist
gives 2 points in high priority and 1 in low
.myParent ul#leftlist
gives 1 points in high priority, 1 point in fair priority and 1 point in low priority
Thus you can differentiate the priority for your selectors in the stylesheet !-)
The function changes to:
function myFunction(element){
var liArray = document.getElementById("leftlist").childNodes;
var i=0, item;
while (item = liArray[i++]) {
if (item.nodeType === 1) {
item.className = item.className.replace(/(^| )current( |$)/g, '');
}
}
element.className += " current";
}
The CSS needs a change to class as well:
ul#leftlist li.current a { ... }
本文标签: htmlJavaScript function to 39get element by parent class39 and assignStack Overflow
版权声明:本文标题:html - JavaScript function to 'get element by parent class' and assign - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745257336a2650180.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论