如何用js设置a的style?

如何用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对象,然后使用a的style对象属性,比如
a.style["backgroundcolor"]="red";

作者: imbiss   发布时间: 2007-04-17

把a.link,a.visited,a.hover这些的样式都写到css里面,用js动态加载css?

作者: alecz1   发布时间: 2007-04-19

写css里赋class,或者js,onmouseover,onmouseout

作者: 3227049   发布时间: 2007-04-19

赞同3楼观点

作者: netyang   发布时间: 2007-04-19



QUOTE:
原帖由 imbiss 于 2007-4-17 03:20 发表
先取得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

jquery试试看

作者: wuwei517200   发布时间: 2007-05-06



[Copy to clipboard] [ - ]
CODE:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<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

错误: $("testlink").classNames is not a function
源文件:http://localhost/test.html
行:34

IE显示脚本错误,FF中显示如上提示。已导入了prototype.js

作者: gydoesit   发布时间: 2007-05-07



QUOTE:
原帖由 gydoesit 于 2007-5-7 08:57 发表
错误: $("testlink").classNames is not a function
源文件:http://localhost/test.html
行:34

IE显示脚本错误,FF中显示如上提示。已导入了prototype.js

作者: HonestQiao   发布时间: 2007-05-07