[英]Modify CSS rule of inner tag with Javascript
I have something like this: 我有这样的事情:
-HTML- -HTML-
<table>
<tr class="x" onMouseOver="light(this)">
<td>
<a href="x">Link</a>
</td>
<td>
Text
</td>
</tr>
</table>
-CSS- -CSS-
.x a{
color: black;
}
-Javascript- -Javascript-
function light(x){
x.style.color="red";
}
Now, the function works correctly, but my a tag doesn't changes his color. 现在,该功能可以正常使用,但是我的标签不会更改其颜色。 Is there a way to make Javascript modify the attribute color of the CSS rule .xa? 有没有办法让Javascript修改CSS规则.xa的属性颜色?
This should do the trick 这应该可以解决问题
x.children[0].children[0].style.color="red";
You can define a :hover
style in the CSS for the link too 您也可以在CSS中为链接定义:hover
样式
a:hover {
color: red;
}
That will change the links color when you hover over it (the link). 当您将鼠标悬停在链接(链接)上时,它将更改链接的颜色。
.x:hover a { }
will apply a style to the link when you hover over .x
悬停在.x
时,将对链接应用样式
This might be more suitable: 这可能更合适:
.x:hover {color:red}
.x:hover a{color:red}
This will override the link's colour when the row is hovered over, making the onMouseOver
function unnecessary. 当鼠标悬停在行上时,它将覆盖链接的颜色,从而无需onMouseOver
函数。
Note that it may fail in IE6, but personally I've never had any problems making things other than links :hover
in that browser... 请注意,它可能不会在IE6中失败,但是就我个人而言,除了在浏览器中进行链接:hover
,我从未遇到任何其他问题。
This will change all the links that are children to red. 这会将所有子链接变为红色。
function light(x){
var elems = x.getElementsByTagName("a");
for(var i=0; i < elems.length;i++){
elems[i].style.color="red";
}
}
Although I highly suggest using the :hover
psuedo class like others have mentioned. 尽管我强烈建议像其他人一样使用:hover
psuedo类。
-HTML- wrap your tr with in <table>
tag, and convert to lowercase your onmouseover
event attribute. -HTML-用<table>
标记包装tr,并将onmouseover
事件属性转换为小写。
<table>
<tr class="x" onmouseover="light(this)">
<td>
<a href="x">Link</a>
</td>
<td>
Text
</td>
</tr>
</table>
-Javascript- change you javascript function body for select to a
tag -Javascript-改变你的JavaScript函数体选择a
标签
function light(x){
x.getElementsByTagName("a")[0].style.color="red";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.