この記事の例では、JSによるインラインスタイルを取得する方法について説明します。参照のためにそれを共有してください。特定の実装方法は次のとおりです。
次のようにコードをコピーします:<!doctype html>
<html lang = "en">
<head>
<メタcharset = "utf-8">
<meta name = "Viewport" content = "width = device-width、target-dencessdpi = high-dpi、high-scale = 1.0、minimut-scale = 1.0、maximing-scale = 1.0、user-scalable = no"/>
<Title> JavaScriptはインラインスタイルを取得します</title>
</head>
<style>
#箱{
幅:100px;
高さ:100px;
背景画像:url(1.jpg);
バックグラウンドサイズ:140%140%;
バックグラウンドリピート:ノーリピート。
国境:20pxソリッドRGBA(0,0,0、.3);
バックグラウンドクリップ:ボーダーボックス。
バックグラウンドポジション:-20px -20px;
}
</style>
<body>
<div id = "box"> </div>
</body>
</html>
<スクリプト>
//インラインスタイル要素を取得します
関数getStyle(obj、attr)
{
if(window.getComputedStyle){
return getComputedStyle(obj、null)[attr];
}それ以外{
obj.currentStyle [attr]を返します。
}
}
//オブジェクトを取得します
function $(id){
document.getElementByID(ID)を返します。
}
var box = $( 'box');
//印刷スタイル
アラート(getStyle(box、 'background-position'));
</script>
この記事がみんなのJavaScriptプログラミングに役立つことを願っています。