0

ので、私は持っています

<span onclick="show(this)">foobar</span>

function show(theSpan) {
    ....... //select the span here
}

Jqueryセレクターを使用してスパンを選択するにはどうすればよいですか? $(theSpan) を試しましたが、うまくいきません。attr() を適用できるように、Jquery オブジェクトとして選択したいと考えています。

ありがとう!

4

2 に答える 2

1

$(theSpan).attr('test, '123');正常に動作します。

あなたの問題は別の場所にあります。

于 2012-07-13T20:23:43.037 に答える
0

要素には属性はありませんが、例を挙げると、次のようになったとします。

<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
于 2012-07-13T20:33:58.763 に答える