便利なコマンド

withとthisの紹介です。

with

省略できます。
with(共通のオブジェクト等){
}

document.write(123);
document.write("abc");

を、

with(document){
write(123);
write("abc");
}

と書くことができます。

もうひとつ、例です。

<span id="wor">cherry</span>
<script type="text/javascript">
<!--
with(wor.style){
color="red";
backgroundColor="black";
}
-->
</script>
cherry

this

そのフォーム等を指し示します

<button onClick="this.style.backgroundColor='white'">change</button>

これは、次のように書くのと同じです。

<button id="ac" onClick="ac.style.backgroundColor='white'">change</button>

もう1つ例です。

<p>
<span onMouseover="this.style.color='red'" onMouseout="this.style.color='black'">
change-x</span>
</p>

change-x

Next : 次のステップへ
Pre  : 前のステップへ
 Up  : JavaScriptに戻る
Home : ホームに戻る

Presented by Rei

テレワークならECナビ Yahoo 楽天 LINEがデータ消費ゼロで月額500円〜!
無料ホームページ 無料のクレジットカード 海外格安航空券 海外旅行保険が無料! 海外ホテル