日期:2014-05-16  浏览次数:20436 次

想动态设置样式,不知道怎么写
想动态设置样式,下面这样写不行,请问要怎么写?
<div id="d1" style="font-weight:bold"></div>
<script language="javascript">
d1.setAttribute('style', 'font-weight: bold; font-size: 11px');
</script>

------解决方案--------------------
当然不行,style 是对象属性,无法直接设置!
L@_@K
HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title> new document </title>
    <meta name="generator" content="editplus" />
    <meta name="author" content="Gao YiXiang" />
    <meta name="email" content="yixianggao@126.com" />
    <meta name="keywords" content="javascript dhtml dom" />
    <meta name="description" content="I love web development." />
</head>
<body>
    <div id="d1">Dynamic Style</div> 
    <script   language="javascript"> 
    d1.style.fontWeight = 'bold'; 
    d1.style.fontSize = "20px";
    </script> 
</body>
</html>