我在这里见过。tbl
以下语句中的含义是什么?这意味着什么?
var rows = $('tr', tbl);
上面的tbl
那个是另一个dom元素。这是作为(可选参数)传入的context
:
jQuery( selector [, context ] )
...对于selector
, 在这种情况下'tr'
。
所以本质上是这样的:
$('tr', tbl);
说返回我与'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
此模式使用 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中使用 jQuery context 有很好的解释