DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> js獲取內聯樣式的方法教程
js獲取內聯樣式的方法教程
編輯:關於JavaScript     

本文實例講述了js獲取內聯樣式的方法。分享給大家供大家參考。具體實現方法如下:

代碼如下:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,target-densitydpi=high-dpi,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>javascript 獲取內聯樣式</title>
</head>

<style>
#box{
width:100px;
height:100px;
background-image:url(1.jpg);
background-size:140% 140%;
background-repeat:no-repeat;
border:20px solid rgba(0,0,0,.3);
background-clip:border-box;
background-position:-20px -20px;
}
</style>
<body>
<div id="box"></div>
</body>
</html>
<script>
//獲取內聯樣式元素
function getStyle( obj , attr )
{
if ( window.getComputedStyle ) {
return getComputedStyle( obj , null )[attr];
}else{
return obj.currentStyle[attr];
}
}
//獲取對象
function $(id){
return document.getElementById(id);
}
var box = $('box');

//打印樣式
alert(getStyle(box , 'background-position'));
</script>

希望本文所述對大家的javascript程序設計有所幫助。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved