ので、私は持っています
<span onclick="show(this)">foobar</span>
function show(theSpan) {
....... //select the span here
}
Jqueryセレクターを使用してスパンを選択するにはどうすればよいですか? $(theSpan) を試しましたが、うまくいきません。attr() を適用できるように、Jquery オブジェクトとして選択したいと考えています。
ありがとう!
ので、私は持っています
<span onclick="show(this)">foobar</span>
function show(theSpan) {
....... //select the span here
}
Jqueryセレクターを使用してスパンを選択するにはどうすればよいですか? $(theSpan) を試しましたが、うまくいきません。attr() を適用できるように、Jquery オブジェクトとして選択したいと考えています。
ありがとう!
$(theSpan).attr('test, '123');
正常に動作します。
あなたの問題は別の場所にあります。
要素には属性はありませんが、例を挙げると、次のようになったとします。
<span id="spTest" onclick="show(this);">foobar</span>
これは、ID のような属性を取得する方法です。
var id = $(theSpan).attr('id'); //outputs: spTest
しかし、あなたの質問であなたが何を意味するのかわかりません。おそらく値が必要なので、これがそれを取得する方法です:
var spanValue = $(theSpan).html(); //outputs: foobar
また、その要素に id を設定しようとしている場合は、次のことができます。
//Set the id
$(theSpan).attr('id', 'theSpan');
//Store the value from your element by the new id
var spanValue = $('#theSpan').html();
//Do what you want at this point
alert(spanValue); //outputs: foobar