我想通过DOM+javascript来修改一个标签的内联样式
我们知道每个标签都有style属性(内联样式),我是否可以这样,把a标签的颜色变红?<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
function onMouseDown()
{var x=document.getElementById("aaa").style="color:red";
}
</script>
</head><body>
<a href="#" id="aaa">aa</a>
<input type="button" onmousedown="onMouseDown()">
</body>
</html>

解决方案 »

  1.   

    我是楼主修改一下
    function onMouseDown()
    {document.getElementById("aaa").style="color:red";
    }
      

  2.   

    document.getElementById("aaa").style.backgroundColor= "red";
      

  3.   

    你也可以先在css中定义好class
    再:document.getElementById("aaa").className='class1';
      

  4.   

    最简单的方式:
    document.getElementById("aaa").style.color = 'red';
      

  5.   


    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript">
    function onMouseDown()
    {
    var x=document.getElementById("aaa").style.color="red";
    alert(x)
    }
    </script>
    </head><body>
    <a href="#" id="aaa">aa</a>
    <input type="button" onmousedown="onMouseDown()">
    </body>
    </html>直接 document.getElementById("aaa").style.color="red" 就okhttp://www.w3school.com.cn/b.asp
    楼主可以看下 html dom 及DHTML
      

  6.   

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript" src="http://s.syyx.com/nycs/js/jquery-1.6.1.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
    $(":button").click(function(){
    alert("11111");
    $("#aaa").css({"color":"red" });
    })
    })
    </script>
    </head><body>
    <a href="#" id="aaa"  style=" color:#F0F;">我要变颜色</a>
    <input type="button" value="有本事点我啊"></input>
    </body>
    </html>
      

  7.   

    楼主的问题答案是肯定的,可以通过上面那个方法定义style样式,规范点,可以加一个addLoadEvent函数,去避免加载过程中js同DOM的加载顺序。另外获取元素节点style属性,只认直接定义在元素节点上的style="...."同通过DOM加进去的,不认外部css文件同<style>..</style>加进去的。