1

私はここでそれを見ました。tbl次の文の意味は何ですか? それは何を意味しますか?

var rows = $('tr', tbl);
4

2 に答える 2

4

上記tblの は別の dom 要素です。これは (オプションのパラメーター) として渡されますcontext

jQuery( selector [, context ] )

... selector、この場合は'tr'.

ソース


だから本質的にこれ:

$('tr', tbl);

element(s) のセレクターに一致するすべてのものを返すと言い'tr'tblます。


与えられた

<table>
  <tr>first</tr>
<table>
<table id="test">
   <tr>second</tr>
</table>

これはさまざまな結果を返します。

//context is global
$('tr') => first & second

//restrict the context to just the second table 
//by finding it and passing it into the selector
var tbl = $('#test');
$('tr', tbl) => just second
于 2016-05-16T15:05:29.120 に答える
2

このパターンは jQuery コンテキストを使用しています。クエリは、テーブル内の行を見つけるために使用されます。

var tbl = $("table#tableId"); // this line provides the context
var rows = $("tr", tbl); // finding all rows within the context

これは、書き込みに相当します。

var rows = tbl.find("tr")

このSO Question hereでjQueryコンテキストを使用することについての良い説明があります

于 2016-05-16T15:01:17.847 に答える