いくつかの問題:
- を忘れました
}
。
- jQuery オブジェクトにラップされていない要素で jQuery メソッドを呼び出しています。あなたがする必要があります:
$(itemName.getElementsByTagName("span")[0]).show("slow");
(ラッピングにご注意ください。) jQuery メソッドは魔法のようにデフォルトの要素を拡張するわけではありません。最初にオブジェクトをラップする必要があります。
このバージョンが動作することに注意してください。
編集:
または、jQuery のコンストラクト (スコープ) の 2 番目のパラメーターを使用して、このコードを短くすることもできます。
function showy(itemName) {
$('span:first',itemName).show("slow");
}
function closy(itemName) {
$('span:first',itemName).hide("slow");
}
EDITv2
Juan が良い点を指摘してくれました。javascript とマークアップを分離する必要もあります。つまり、要素の on* 属性の使用を避け、バインディングを外部の .js ファイルまたは<script>
タグ内に保持します。例えば
<head>
...
<script src="http://path.to/jquery.js">
<script>
$(function(){ // execute once the document is ready (onload="below_code()")
// bind to the buttons' hover events
// find them by the "button" and "white" class names
$('.button.white').hover(function(){ // hover event (onmouseover="below_code()")
// find the first span within the link that triggered the event
$('span:first',this).show('slow');
},function(){ // mouse out event (onmouseout="below_code()")
// likewise, find first span
$('span:first',this).hide('slow');
});
});
</script>
...
</head>
<body>
...
<a href="#" class="button white" id="button1">
<span id="spanToShow">SHOW: this text </span>
on hover
</a>
...
</body>