js修改css样式

修改宽度:

document.getElementById("div1").style.width="200px";

 

修改border-radius:

document.getElementById("div1").style.borderRadius="100px";

 

修改背景图片:

document.getElementById("div1").style.backgroundImage='url(img.jpg)';

 

或者:

document.getElementById("div1").style.cssText="border-radius:100px; width: 200px; background-image: url(img.jpg)";

 

注意:

1:用style.属性名="属性值" 的时候属性名可能要改变一下,比如border-radius要变为borderRadius;background-image要变为backgroundImage;

  但是用style.cssText的时候里面属性名的写法和css的写法相同。

2:两种方法最好不要混用,当用style.cssText设置之后,写在这句代码之前的style.width等等将会失效。

 

CSS参考手册:http://www.ziqiangxuetang.com/cssref/css-reference.html

       http://www.w3school.com.cn/h.asp

posted on 2016-04-30 08:33  辉子t1  阅读(402)  评论(0编辑  收藏  举报

导航