想动态设置样式,不知道怎么写

awuZhang 2007-12-05 02:59:48
想动态设置样式,下面这样写不行,请问要怎么写?
<div id="d1" style="font-weight:bold"></div>
<script language="javascript">
d1.setAttribute('style', 'font-weight: bold; font-size: 11px');
</script>
...全文
311 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
awuZhang 2007-12-06
  • 打赏
  • 举报
回复
自己写了个方法

if('style' in param){
var s = param['style'];
var atts = {};
arr = s.split(";");
arr.each( function(e){
att = e.split(":");
atts[att[0].strip().camelize()] = att[1].strip();
})
Object.extend(returnElement.style, atts);
}
yixianggao 2007-12-05
  • 打赏
  • 举报
回复
To awuZhang

当然不行,style 是对象属性,无法直接设置!

不过你可以只考虑修改样式名或替换.css文件!

给个修改样式名滴例子!

<!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." />
<style type="text/css">
.divStyle1
{
font-size:12pt;
}
.divStyle2
{
font-size:20pt;
font-weight: bold;
color: red;
}
</style>
</head>
<body>
<DIV class='divStyle1' onmouseover="this.className='divStyle2';" onmouseout="this.className='divStyle1';">
鼠标移过,字体大小动态变换!
</DIV>
</body>
</html>
awuZhang 2007-12-05
  • 打赏
  • 举报
回复
我自己想到办法了
用 prototype.js 的 camelize()
把 font-size 转成 fontSize
然后 Object.extend(d1.style ,{'style集合'})
我自己先去试试,回头再来结贴 :D
hztgcl1986 2007-12-05
  • 打赏
  • 举报
回复
document.getElementById("d1").setAttribute.style.font-weight="bold";





yixianggao 2007-12-05
  • 打赏
  • 举报
回复
To iori_wen

事实胜于瞎讲,哈
L@_@K
<!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 STYLE="font-size:12pt" onmouseover="this.style.fontSize='20pt'" onmouseout="this.style.fontSize='12pt'">
鼠标移过,字体大小动态变换!
</DIV>
</body>
</html>
awuZhang 2007-12-05
  • 打赏
  • 举报
回复
fontWeight 我也知道啊。。。
只是想问看看能不能用什么方法直接 style='font-weight:bold;font-size:11px'
iori_wen 2007-12-05
  • 打赏
  • 举报
回复
解释:font-size为只读,你改变的样式中不能包含这样的值,像backgroundColor这样的值为可读可写。请给分,谢谢
yixianggao 2007-12-05
  • 打赏
  • 举报
回复
font-weight 也没起作用,
那是标记中 style="font-weight:bold" 起滴作用!
yixianggao 2007-12-05
  • 打赏
  • 举报
回复
当然不行,style 是对象属性,无法直接设置!
L@_@K
<!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>
awuZhang 2007-12-05
  • 打赏
  • 举报
回复
font-size 的值不会变
tantaiyizu 2007-12-05
  • 打赏
  • 举报
回复

怎么不行呢?

87,997

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧