導航:首頁 > 編程語言 > 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相關的資料

熱點內容
網路載入視頻失敗是怎麼回事 瀏覽:805
傳奇賬號在哪個文件夾里 瀏覽:346
百度app在哪裡安裝 瀏覽:587
如何設置路由器網路不斷網 瀏覽:471
傳到qq群里的文件怎麼刪除 瀏覽:861
索尼安卓71更新日誌 瀏覽:234
怎麼找手機里的垃圾app 瀏覽:540
2015藍橋杯代碼填空 瀏覽:698
安卓資料庫dbexecSQL 瀏覽:227
doc重命名文件格式 瀏覽:728
getscreen截圖工具下載 瀏覽:719
共識數據是什麼時候開始的 瀏覽:96
數碼管顯示電壓程序 瀏覽:479
資料庫文件有哪個 瀏覽:543
途強儲存在哪個文件夾 瀏覽:172
如何恢復被覆蓋文件 瀏覽:611
iphone5用哪個版本最好 瀏覽:327
extjsgrid禁用 瀏覽:426
如何查找國外論文的編程代碼 瀏覽:366
暗金顏色代碼 瀏覽:789

友情鏈接