・Jqueryを勉強中。私$('<td/>')はこのコードでこのセレクターを理解していません:
$('<td/>').insertAfter($(this)).text(height).css('border', 'thin solid red');
- だれか教えてくれませんか?
・Jqueryを勉強中。私$('<td/>')はこのコードでこのセレクターを理解していません:
$('<td/>').insertAfter($(this)).text(height).css('border', 'thin solid red');
- だれか教えてくれませんか?
$('<td/>')tag を持つ DOM 要素を作成しますtd。
insertAfter($(this))要素の後に要素を追加しthisます。
.text(height)tdタグ内のテキストを変更します。
最後に.css('border', 'thin solid red');、要素に赤い境界線を適用しtdます。
これをコンポーネントに分解してみましょう。
$('<td/>') // this is initializing a new DOM Node. At this point, however, it's not actually connected to anything, it's just a DOM Node hanging out in the DOM and not attached to the document flow.
.insertAfter($(this)) // now, this is telling jQuery to take that TD element you just created, and insert it after whatever $(this) evaluates to... assuming this is inside, say, a click handler, it would evaluate to the object that triggered the click event, and attach the TD element after "this" element.
.text(height) // says, "set the .text of your TD element to whatever is in the height variable" ... you're basically plugging text inside your TD element.
.css('border', 'thin solid red') // is telling jQuery to modify the TD's style, adding a style for border that is thin, solid and red.
これがどのように機能するかの例については、私が jsFiddle でまとめた例を参照してください。 http://jsfiddle.net/mori57/xLJHx/
(リンク先の jsFiddle をいじってみると、興味深いフォローアップの質問が表示されるはずですが、ここで水を濁したくはありません)
$('<td/>')新しい空の<td>要素を作成し、jQuery オブジェクトにラップします。
純粋な JavaScript で要素を作成し、それを で選択した場合と同じです$('.selector')。