Javascriptをはじめ、Ajax、jQueryの著書多数のガイドが、何かと最近騒がれているJa…
北欧好きが、愛用の北欧モノを見せ合うコミュニティ
Javascript関連情報
更新日:2004年03月18日
DOMの仕様にそってサンプルを連載します。今回はNodeインターフェイスのnodeValueです。文字通り、対象ノードの値を取得または設定するのに使います。
| Interface | nodeName | nodeValue | attributes |
|---|---|---|---|
Attr |
アトリビュート名( Attr.nameと同じ) |
属性値(Attr.valueと同じ) |
null |
CDATASection |
"#cdata-section" |
CDATA セクションの内容 (CharacterData.dataと同じ) |
null |
Comment |
"#comment" |
コメントの内容 (CharacterData.dataと同じ) |
null |
Document |
"#document" |
null |
null |
DocumentFragment |
"#document-fragment" |
null |
null |
DocumentType |
document type 名 DocumentType.name |
null |
null |
Element |
タグ名 Element.tagName |
null |
NamedNodeMap |
Entity |
エンティティ名 entity name | null |
null |
EntityReference |
参照されるエンティティ名 entity referenced | null |
null |
Notation |
表記法名 notation name | null |
null |
ProcessingInstruction |
ターゲット ProcessingInstruction.target |
ProcessingInstruction.dataと同じ |
null |
Text |
"#text" |
テキストノードの内容 (CharacterData.dataと同じ) |
null |
<div id="test"><a href="http://jsgt.org/" target="targetWin">jsGadget</a></div>
<form>
<input type = "button"
value = "hrefは?"
onclick = "att1 = getElementById('test').firstChild.attributes ;
alert(att1.getNamedItem('href').nodeValue)">
<input type = "button"
value = "targetは?"
onclick = "att1 = getElementById('test').firstChild.attributes ;
alert(att1.getNamedItem('target').nodeValue)">
</form>
ここでは、<div id="test">の最初の子ノード(firstChild)であるAタグ内の属性をNamedNodeMapにより取得して 各nodeValueをダイアログ表示しています。
<div id="test"><a href="http://jsgt.org/" target="targetWin">jsGadget</a></div>
<form>
<input type="button"
value="hrefは?"
onclick="alert(getElementById('test').firstChild.getAttribute('href'))">
<input type="button"
value="targetは?"
onclick="alert(getElementById('test').firstChild.getAttribute('target'))">
</form>
<div id="test"><a href="http://jsgt.org/" target="targetWin">jsGadget</a></div>
<form>
<input type="button"
value="hrefは?"
onclick="alert(getElementById('test').firstChild.href)">
<input type="button"
value="targetは?"
onclick="alert(getElementById('test').firstChild.target)">
</form>
<div id="test2"><a href="javascript:alert('url入力してボタンを押してください')">
下のフォームに書いたリンク</a></div>
<form>
<input type = "text"
name = "url2"
value = "http://"
style = "width : 400px"><br>
<input type="button"
value="リンクを書き換えます"
onclick="att1 = getElementById('test2').firstChild.attributes ;
att1.getNamedItem('href').nodeValue = this.form.url2.value">
</form>
ボタンをクリックすると下記の入力フィールドへ書いたURLでAタグ内のhref属性値(nodeValue)を書き換えます。 最初、何もしない状態でリンクをクリックすると、href="javascript:alert('url入力して...のダイアログが出ますが、 入力フィールドへURLを書いてからボタンクリックすると、このhref="javascript:alert('url入力して...の部分 がURLに書き換わり、クリックするとそのURLが読み込まれます。
<script language='JavaScript'>
<!--
function chgImgSrc(oj,src)
{
imgsrc = oj.attributes.getNamedItem('src')
imgsrc.nodeValue = src
}
//-->
</script>
下記画像にマウスカーソルが触れると画像が変わります。
<br>
<br>
<img src = "../CU20040306/plus.gif"
onmouseover = "chgImgSrc(this,'../CU20040306/minus.gif')"
onmouseout = "chgImgSrc(this,'../CU20040306/plus.gif')">
IMGタグの中のonmouseover(カーソルが触れたとき発生),onmouseout(カーソルが離れたとき発生)というイベント ハンドラによって関数chgImgSrc(oj,src)を起動しています。引数ojにはIMGタグ自身がthisによって渡され、引数srcには 書き換えたい画像のURLが渡されます。そして、関数1行目の
<script language='JavaScript'>
<!--
function chgImgSrc2(oj,src)
{
oj.src = src
}
//-->
</script>
下記画像にマウスカーソルが触れると画像が変わります。
<br>
<br>
<img src = "../CU20040306/plus.gif"
onmouseover = "chgImgSrc2(this,'../CU20040306/minus.gif')"
onmouseout = "chgImgSrc2(this,'../CU20040306/plus.gif')">
下記画像にマウスカーソルが触れると画像が変わります。
<img src = "../CU20040306/plus.gif"
onmouseover = "this.src='../CU20040306/minus.gif'"
onmouseout = "this.src='../CU20040306/plus.gif'">
人気Javascriptランキング
Powered by 価格.com
北欧好きが、愛用の北欧モノを見せ合うコミュニティ