导航:首页 > 编程语言 > css样式添加的属性js

css样式添加的属性js

发布时间:2023-05-27 03:54:09

js修改CSS设置的样式

语法:元素.style.样式名=样式值

注意:如果CSS的样式名中含有-,这种名称在JS中是不合法的,比如 background-color 。需要将这种样式名修改为驼峰命名法:去掉-,然后将-后的第一个字母大写,比如 backgroundColor

我们通过style属性设置的样式都是内联样式,而内联样式有较高的优先级,所以通过JS修改的样式往往会立即显示
但是如果在样式中写了!important,则此时样式会有最高的优先级,即使通过JS也不能覆盖该样式,此时将会导致JS修改样式失效
所以尽量不要为样式添加!important

语法:元素.style.样式名
通过style属性设置和读取的都是内联样式,无法读取样式表中的样式

语法:元素.currentStyle.样式名
如果当前元素没有设置该样式,则获取它的默认值
例如: box1.currentStyle.width

这个方法是window的方法,可以直接使用需要两个参数
第一个:要获取样式的元素
第二个:可以传递一个伪元素,一般都传null

该方法会返回一个对象,对象中封装了当前元素对应的样式
可以通过对象﹒样式名来读取样式

如果获取的 样式没有设置 ,则会获取到真实的值,而不是默认值
比如:没有设置width,它不会获取到auto,而是 一个长度

注意:通过currentStyle和getComputedstyle()读取到的样式都是只读的,不能修改,如果要修改必须通过style属性

参数:
obj 要获取样式的元素
name 要获取的样式名

② js向id添加css属性值

jquery语法

$("#id").css("width","360px");

原生语法

varid=document.getElementById("id");
id.style.background=red;

③ 如何使用js操作css属性

onmouseover="this.style.borderColor='red'" onmouseout="this.style.borderColor='grey'"
这句话要加在你的图片上面<img src=""…… onmouseover="this.style.borderColor='red'" onmouseout="this.style.borderColor='grey'" />

④ js 写CSs样式

javaScript访问修改样式表,可以方便的动态修改页面:

一、访问元素中style属性的CSS样式

这个可以直接使用style对象方便的访问,例如:

<div id = "mdiv" style="background-color:blue;">....</div>

访问CSS的方法是:

<script type="text/javascript">

//获得元素

var oDiv = document.getElementyId("mdiv");

//访问元素的style对象,再访问对象中的属性,也可以修改属性的值,直接为他赋值

alert(oDiv.style.backgroundColor);

</script>

二、访问外部定义的CSS样式(类定义的CSS样式)

这个没法使用上面的方法去访问,因为CSS数据不是存储在style属性中,它是存储在类中的。

访问方法:先取得定义类的样式表的引用,用document.styleSheets集合实现这个目的,这个集合包含HTML页面中所有的样式表,DOM为每个样式表定义一个cssRules的集合,这个集合中包含定义在样式表中的所用CSS规则(注意:Mozilla和Safasi中是 cssRules,而IE中式rules)。

例如:

<style>

/*第一条规则*/

div.ss{

background-color:red;

width:101px;

}

/*第二条规则*/

a.btn2{

background:url(imag/2-AccessCtl.jpg);

}

</style>

访问CSS:

var ocssRules = document.styleSheets[0].cssRules||document.styleSheets[0].rules;

//访问第一条规则

alert(ocssRules[0].style.backgroundColor);

//设置值

ocssRules[0].style.width = "992px";

//访问第二条规则

alert(ocssRules[1].style.background);

//设置值

ocssRules[0].style.background = "url(imag/3-back.jpg);";

以上内容是网络出来的

⑤ 如何用JS判断CSS语句后,并添加CSS属性

实际上 你若看了 图中源码的话 就不会问这样的问题了 因为按你说的效果是可以实现的

但是会增加网站体积 需要JS 来实现这个效果是不划算的


图中实际原理是

原理
<ulstyle="width:200px;border-bottom:1pxdotted#999;padding:10px;margin-top:10px;">
<listyle="line-height:30px;font-weight:bold;color:#09F;">1内容内容内容内容内容内容</li>
<listyle="line-height:30px;font-weight:bold;color:#09F;">2内容内容内容内容内容内容</li>
<listyle="line-height:30px;font-weight:bold;color:#09F;">3内容内容内容内容内容内容</li>
<listyle="line-height:30px;font-weight:bold;color:#09F;">4内容内容内容内容内容内容</li>
<listyle="line-height:30px;font-weight:bold;color:#09F;">5内容内容内容内容内容内容</li>
</ul>
<ulstyle="width:200px;border-bottom:1pxdotted#999;padding:10px;margin-top:10px;">
<listyle="line-height:30px;font-weight:bold;color:#09F;">1内容内容内容内容内容内容</li>
<listyle="line-height:30px;font-weight:bold;color:#09F;">2内容内容内容内容内容内容</li>
<listyle="line-height:30px;font-weight:bold;color:#09F;">3内容内容内容内容内容内容</li>
<listyle="line-height:30px;font-weight:bold;color:#09F;">4内容内容内容内容内容内容</li>
<listyle="line-height:30px;font-weight:bold;color:#09F;">5内容内容内容内容内容内容</li>
</ul>
<ulstyle="width:200px;border-bottom:1pxdotted#999;padding:10px;margin-top:10px;">
<listyle="line-height:30px;font-weight:bold;color:#09F;">1内容内容内容内容内容内容</li>
<listyle="line-height:30px;font-weight:bold;color:#09F;">2内容内容内容内容内容内容</li>
<listyle="line-height:30px;font-weight:bold;color:#09F;">3内容内容内容内容内容内容</li>
<listyle="line-height:30px;font-weight:bold;color:#09F;">4内容内容内容内容内容内容</li>
<listyle="line-height:30px;font-weight:bold;color:#09F;">5内容内容内容内容内容内容</li>
</ul>
<ulstyle="width:200px;border-bottom:1pxdotted#999;padding:10px;margin-top:10px;">
<listyle="line-height:30px;font-weight:bold;color:#09F;">1内容内容内容内容内容内容</li>
<listyle="line-height:30px;font-weight:bold;color:#09F;">2内容内容内容内容内容内容</li>
<listyle="line-height:30px;font-weight:bold;color:#09F;">3内容内容内容内容内容内容</li>
<listyle="line-height:30px;font-weight:bold;color:#09F;">4内容内容内容内容内容内容</li>
<listyle="line-height:30px;font-weight:bold;color:#09F;">5内容内容内容内容内容内容</li>
</ul>
<ulstyle="width:200px;border-bottom:1pxdotted#999;padding:10px;margin-top:10px;">
<listyle="line-height:30px;font-weight:bold;color:#09F;">1内容内容内容内容内容内容</li>
<listyle="line-height:30px;font-weight:bold;color:#09F;">2内容内容内容内容内容内容</li>
<listyle="line-height:30px;font-weight:bold;color:#09F;">3内容内容内容内容内容内容</li>
<listyle="line-height:30px;font-weight:bold;color:#09F;">4内容内容内容内容内容内容</li>
<listyle="line-height:30px;font-weight:bold;color:#09F;">5内容内容内容内容内容内容</li>
</ul>

方案一
<php>
循环
<ulstyle="width:200px;border-bottom:1pxdotted#999;padding:10px;margin-top:10px;">
子循环
<li{如果i=1orid=6ori=11...看你需要来进行判断每个UL第一个li如果是第一个就加粗}style="line-height:30px;font-weight:bold;color:#09F;"{else}style="line-height:30px;font-weight:bold;color:#09F;"{end}>1内容内容内容内容内容内容</li>
子循环结束
</ul>
循环结束
<php>

方案二
<php>
循环
{如果i=1orid=6ori=11...看你需要来进行判断UL需不需要显示}
<ulstyle="width:200px;border-bottom:1pxdotted#999;padding:10px;margin-top:10px;">{end}
<li{如果i=1orid=6ori=11...看你需要来进行判断每个UL第一个li如果是第一个就加粗}style="line-height:30px;font-weight:bold;color:#09F;"{else}style="line-height:30px;font-weight:bold;color:#09F;"{end}>1内容内容内容内容内容内容</li>
{如果i=5orid=10ori=115...看你需要来进行判断/UL需不需要显示}</ul>{end}
循环结束
<php>

因为不知道你用什么语言来写 所以我大概的写个样子 你看懂这个流程就可以了

⑥ js如何添加CSS样式中的class添加属性

js不可复修改css中的属性,只制能为指定的class的元素添加内联样式(style)
原生JS:
var dom = document.getElementsByClassName('dtd');
for(var i=0,len=dom.length; i<len; i++){
dom[i].style.color = 'red';
}Jquery
$('.dtd').css({color:'red'});

⑦ 如果div原来用ID定义了css样式可以再用js重新添加css样式吗,怎么添加

可以的. 可以这样: document.getElementById("id").style.height = 100px;
.style.后面可以跟升握css的属性名, 如宽高, 背吵庆庆景颜色等等, 具体你可以去尝试一下. 当原来已有此属性时,再用js设置就会把原有的属性差橘覆盖.

⑧ 如何用JS来改变CSS属性

通过js来改变CSS属性,使用jQuery可以很方便的实现,像这样:

$("img").css('border-color','red');

就可以把边框颜色都变成红色。

这是针对此问题的测试页面

⑨ js中巧用cssText属性批量操作样式

给一个HTML元素设置css属性,如
复制代码
代码如下:
var
head=
document.getElementById("head");
head.style.width
=
"200px";
head.style.height
=
"70px";
head.style.display
=
"block";
这样写太罗嗦了,为了简单些写个工具函数,如
复制代码
代码如下:
function
setStyle(obj,css){
for(var
atr
in
css){
obj.style[atr]
=
css[atr];
}
}
var
head=
document.getElementById("head");
setStyle(head,{width:"200px",height:"70px",display:"block"})
发现
Google
API
中使用了cssText属性,后在各浏览器中测试都通过了。一行代码即可,实在很妙。如
复制代码
代码如下:
var
head=
document.getElementById("head");
head.style.cssText="width:200px;height:70px;display:bolck";
和innerHTML一样,cssText很快捷且所有浏览器都支持。此外当批量操作样式时,cssText只需一次reflow,提高了页面渲染性能。
但cssText也有个缺点,会覆盖之前的样式。如
复制代码
代码如下:
<div
style="color:red;">TEST</div>
想给该div在添加个css属性width
复制代码
代码如下:
div.style.cssText
=
"width:200px;";
这时虽然width应用上了,但之前的color被覆盖丢失了。因此使用cssText时应该采用叠加的方式以保留原有的样式。
复制代码
代码如下:
function
setStyle(el,
strCss){
var
sty
=
el.style;
sty.cssText
=
sty.cssText
+
strCss;
}
使用该方法在IE9/Firefox/Safari/Chrome/Opera中没什么问题,但由于
IE6/7/8中cssText返回值少了分号
会让你失望。
因此对IE6/7/8还需单独处理下,如果cssText返回值没";"则补上
复制代码
代码如下:
function
setStyle(el,
strCss){
function
endsWith(str,
suffix)
{
var
l
=
str.length
-
suffix.length;
return
l
>=
0
&&
str.indexOf(suffix,
l)
==
l;
}
var
sty
=
el.style,
cssText
=
sty.cssText;
if(!endsWith(cssText,
';')){
cssText
+=
';';
}
sty.cssText
=
cssText
+
strCss;
}
相关:
http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration
https://developer.mozilla.org/en/DOM/CSSStyleDeclaration

⑩ 救命,如何用js给div标签添加css属性,当鼠标事件发生的时候,譬如点击某个元素

需要准备的抄材料分别有:电脑、袭html编辑器、浏览器。

1、首先,打开html编辑器,新建html文件,例如:index.html,编写问题基础代码。

阅读全文

与css样式添加的属性js相关的资料

热点内容
40岁男人适合的微信名 浏览:925
编程里比例怎么打 浏览:215
苹果12两个app如何分屏 浏览:592
ps下载完不是压缩文件 浏览:362
电脑中的个人文件包括什么 浏览:572
网络连接一般什么密码 浏览:199
java定时器quartz实例 浏览:259
稻壳excel文件太大怎么弄 浏览:901
文件里的视频如何保存到相册 浏览:428
手机百度云文件电脑 浏览:957
编程怎么做到时钟精准 浏览:912
锤子用过的壁纸在哪个文件里 浏览:468
qq网站安全性未知访问不了怎么办 浏览:270
燕秀怎么修改编程人名字 浏览:789
2012年天之眼导航升级 浏览:595
如何安装视频文件 浏览:315
红米2A升级miui9 浏览:927
如何在表格中加入一行数据库 浏览:421
dnf远古二能升级85吗 浏览:251
extjsbbar属性 浏览:355

友情链接