js獲取某元素的class里面的css屬性值代碼
用js如何獲取div中css的 margin、padding、height、border等。你可能說可以直接用document.getElementById("id").style.margin獲取。但是你說的只能獲取直接在標(biāo)簽中寫的style的屬性,無法獲取標(biāo)簽style外的屬性(如css文件中的屬性)。而下面方法則兩者值都可以獲取。
實(shí)例效果圖如下:
js在獲取css屬性時(shí)如果標(biāo)簽中無style則無法直接獲取css中的屬性,所以需要一個(gè)方法可以做到這點(diǎn)。
getStyle(obj,attr) 調(diào)用方法說明:obj為對(duì)像,attr為屬性名必須兼容js中的寫法(可以參考:JS可以控制樣式的名稱寫法)。
Js代碼
function getStyle(obj,attr){
var ie = !+"\v1";//簡(jiǎn)單判斷ie6~8
if(attr=="backgroundPosition"){//IE6~8不兼容backgroundPosition寫法,識(shí)別backgroundPositionX/Y
if(ie){
return obj.currentStyle.backgroundPositionX +" "+obj.currentStyle.backgroundPositionY;
}
}
if(obj.currentStyle){
return obj.currentStyle[attr];
}
else{
return document.defaultView.getComputedStyle(obj,null)[attr];
}
}
完整實(shí)例測(cè)試代碼:
Html代碼
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>js獲取某元素的class里面的css屬性值</title>
<style>
#box1{margin:5px;padding:5px;height:100px;width:200px;}
a{border:1px solid #ccc;border-radius:3px;padding:3px 5px;margin:5px 0;display:inline-block;background:#eee;color:#f60;text-decoration:none;font-size:12px;}
a:hover{color:#ff0000;background:#fff;}
</style>
</head>
<body>
<div id="box1">box1的css.#box1{margin:5px;padding:5px;height:100px;width:200px;}</div>
<a href="javascript:;" onclick="getcss('marginTop')">獲取box1的margin-top</a><br />
<a href="javascript:;" onclick="getcss('paddingTop')">獲取box1的padding-top</a><br />
<a href="javascript:;" onclick="getcss('height')">獲取box1的height</a><br />
<script>
//獲取class里面的屬性值
var divs=document.getElementById("box1");
function getStyle(obj,attr){
var ie = !+"\v1";//簡(jiǎn)單判斷ie6~8
if(attr=="backgroundPosition"){//IE6~8不兼容backgroundPosition寫法,識(shí)別backgroundPositionX/Y
if(ie){
return obj.currentStyle.backgroundPositionX +" "+obj.currentStyle.backgroundPositionY;
}
}
if(obj.currentStyle){
return obj.currentStyle[attr];
}
else{
return document.defaultView.getComputedStyle(obj,null)[attr];
}
}
function getcss(typ){
alert(getStyle(divs,typ));
}
</script>
</body>
</html>
- js中設(shè)置元素class的三種方法小結(jié)
- js獲取class的所有元素
- javaScript給元素添加多個(gè)class的簡(jiǎn)單實(shí)現(xiàn)
- js通過元素class名字獲取元素集合的具體實(shí)現(xiàn)
- js選取多個(gè)或單個(gè)元素的實(shí)現(xiàn)代碼(用class)
- 基于原生js實(shí)現(xiàn)判斷元素是否有指定class名
- js 獲取class的元素的方法 以及創(chuàng)建方法getElementsByClassName
- javascript通過class來獲取元素實(shí)現(xiàn)代碼
- js如何根據(jù)class獲取元素并且點(diǎn)擊元素詳解
相關(guān)文章
javascript從右邊截取指定字符串的三種實(shí)現(xiàn)方法
這篇文章主要介紹了javascript從右邊截取指定字符串的三種實(shí)現(xiàn)方法。需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-11-11
原生js的數(shù)組除重復(fù)簡(jiǎn)單實(shí)例
下面小編就為大家?guī)硪黄鷍s的數(shù)組除重復(fù)簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05
Bootstrap學(xué)習(xí)筆記之css組件(3)
這篇文章主要為大家詳細(xì)介紹了bootstrap學(xué)習(xí)筆記中的css組件,感興趣的小伙伴們可以參考一下2016-06-06
js獲取所有checkbox的值的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)硪黄猨s獲取所有checkbox值的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05
微信小程序使用checkbox顯示多項(xiàng)選擇框功能【附源碼下載】
這篇文章主要介紹了微信小程序使用checkbox顯示多項(xiàng)選擇框功能,涉及相關(guān)事件綁定與元素遍歷操作技巧,并附帶源碼供讀者下載參考,需要的朋友可以參考下2017-12-12
js純數(shù)字逐一停止顯示效果的實(shí)現(xiàn)代碼
下面小編就為大家?guī)硪黄猨s純數(shù)字逐一停止顯示效果的實(shí)現(xiàn)代碼。小編覺得非常不錯(cuò)。現(xiàn)在分享給大家。給大家一個(gè)參考2016-03-03
JavaScript 中如何實(shí)現(xiàn)大文件并行下載
本文將介紹如何利用 async-pool 這個(gè)庫(kù)提供的 asyncPool 函數(shù)來實(shí)現(xiàn)大文件的并行下載。2021-05-05

