如何用js设置a的style?
在网上看设置style的js为
var spanElement = document.getElementById(“mySpan”);
spanElement.setAttribute(“style”, “font-weight:bold; color:red;”);
spanElement.style.cssText = “font-weight:bold; color:red;”;
该文章转载自'大智の博客':http://www.csafe.cn/article.asp?id=1277
但是<a href="#">link</a>
这样的超链接,有几个样式,a.link,a.visited,a.hover等,又该如何设置??
作者: gydoesit 发布时间: 2007-04-16
a.style["backgroundcolor"]="red";
作者: imbiss 发布时间: 2007-04-17
作者: alecz1 发布时间: 2007-04-19
作者: 3227049 发布时间: 2007-04-19
作者: netyang 发布时间: 2007-04-19
先取得a对象,然后使用a的style对象属性,比如
a.style["backgroundcolor"]="red";
不行呀,我如果要设color怎么办?a.style['color'] ???问题是link,hover,visited的时候color要不一样的哟
再回其他楼,
先写class的作法我早试过了,不管用。用js在后面改变a的class,样式并不会跟着改。所以需要强制用js设置style
作者: gydoesit 发布时间: 2007-04-23
作者: gydoesit 发布时间: 2007-04-25
作者: gydoesit 发布时间: 2007-05-01
作者: gydoesit 发布时间: 2007-05-06
作者: wuwei517200 发布时间: 2007-05-06
<HTML>
<HEAD>
<TITLE> Test </TITLE>
<style>
a:link { text-decoration: none; color:blue }
a:visited { text-decoration: none; color:gray }
a:active { text-decoration: none; color:green }
a:hover { text-decoration: none; color:red }
a.a1:link { text-decoration: underline; color:blue }
a.a1:visited { text-decoration: underline; color:gray }
a.a1:active { text-decoration: underline; color:green }
a.a1:hover { text-decoration: underline; color:red }
a.a2:link { text-decoration: none; color:blue }
a.a2:visited { text-decoration: none; color:gray }
a.a2:active { text-decoration: none; color:green }
a.a2:hover { text-decoration: none; color:red }
</style>
</HEAD>
<BODY>
<a href="test.html" id="testlink">test</a>
<br><br><br><br>
<button onclick="addCN('a1');">Underline</button>
<button onclick="addCN('a2');">None</button>
<SCRIPT LANGUAGE="JavaScript" src="prototype.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
<!--
function addCN(cnname)
{
var cnary = $('testlink').classNames().toArray();
for(var i=0;i<cnary.length;i++)
{
$('testlink').removeClassName(cnary[i]);
}
$('testlink').addClassName(cnname);
}
//-->
</SCRIPT>
</BODY>
</HTML>
作者: HonestQiao 发布时间: 2007-05-06
源文件:http://localhost/test.html
行:34
IE显示脚本错误,FF中显示如上提示。已导入了prototype.js
作者: gydoesit 发布时间: 2007-05-07
错误: $("testlink").classNames is not a function
源文件:http://localhost/test.html
行:34
IE显示脚本错误,FF中显示如上提示。已导入了prototype.js
作者: HonestQiao 发布时间: 2007-05-07