Javascriptをはじめ、Ajax、jQueryの著書多数のガイドが、何かと最近騒がれているJa…
北欧好きが、愛用の北欧モノを見せ合うコミュニティ
Javascript関連情報
更新日:2004年06月20日
対象がエレメントノードの場合に、そのエレメントが含む属性のリスト。これを利用して、タグ内の各属性にアクセスできます。
1.document.getElementById("a").id
2.document.getElementById("a").getAttribute("id")
3.document.getElementById("a").attributes.id.value
4.document.getElementById("a").attributes.id.nodeValue
5.document.getElementById("a").attributes.item("id").value
6.document.getElementById("a").attributes.getNamedItem("id").value
7.document.getElementById("a").attributes.getNamedItem("id").nodeValue
8.document.getElementsByTagName("div").item(0).id
9.document.getElementsByTagName("div").item(0).getAttribute("id")
10.document.all("a").getAttribute("id")
凡例 Win n7 -- NetscapeNavogator 7.x m1 -- Mozilla1.x e5 -- Internet Explorer 5.x e6 -- Internet Explorer 6.x o7 -- Opera 7.0 Mac n7 -- NetscapeNavogator 7.x m1 -- Mozilla1.x e5 -- Internet Explorer 5.0 または 5.1 s1 -- Safari Linux n7 -- NetscapeNavogator 7.x m1 -- Mozilla1.x k3 -- Konqueror 3.x
| win | mac | linux | |||||||||
| n7 | m1 | e5 | e6 | o7 | n7 | m1 | e5 | s1 | n7 | m1 | k3 |
| ○ | ○ | ○ | ○ | ○ | ○ | ○ | ○ | ○ | ○ | ○ | ○ |
この文字をクリックするとid名を表示します
<p id="abc"
onclick="alert(this.attributes.getNamedItem('id').nodeValue)"
>この文字をクリックするとid名を表示します</p>
この場合id名は「abc」なのでそれが表示されます。getNamedItem('属性名')というメソッドは/Core/NamedNodeMap で仕様化されていて'属性名'によって指定された属性ノードを返します。 | win | mac | linux | |||||||||
| n7 | m1 | e5 | e6 | o7 | n7 | m1 | e5 | s1 | n7 | m1 | k3 |
| ○ | ○ | × | ○ | ○ | ○ | ○ | ○ | ○ | ○ | ○ | × |
<script type="text/javascript">
<!--
function removeTitle(idName)
{
//1 id名がidNameのエレメントを変数img2へ入れる
img2 = document.getElementById(idName)
//2 width属性をimg2のマップから取り除く
img2.attributes.removeNamedItem("title")
}
function setTitle(idName,titleText)
{
//1 id名がidNameのエレメントを変数img2へ入れる
img2 = document.getElementById(idName)
//2 titleと言う名前の属性を生成し変数attriへ入れる
attri = document.createAttribute("title")
//3 title属性をimg2のマップにセットする
img2.attributes.setNamedItem(attri)
//4 セットされたtitle属性に値を書き込む
img2.setAttribute("title",titleText)
}
//-->
</script>
<br>
<img id = "sample2"
title = "これはコーヒーカップです"
src = "//img.allabout.co.jp/gm/article/24075/cup.jpg">
<form>
<input type = "button"
value = "画像のtitleを除去"
onclick = "removeTitle('sample2')"><br>
<input type = "button"
value = "titleをセット「ティーカップでもあるよ」"
onclick = "setTitle('sample2','ティーカップでもあるよ')"><br>
<input type = "button"
value = "titleをセット「これは実は子供のおもちゃだったりもします」"
onclick = "setTitle('sample2','これは実は子供のおもちゃだったりもします')">
</form>
人気Javascriptランキング
Powered by 価格.com
北欧好きが、愛用の北欧モノを見せ合うコミュニティ