所以这是HTML:
<table class="table">
<tr>
<th>Doober durr</th>
<th>Skippi doober</th>
<th>Doober duck</th>
<th>Doober McD</th>
</tr>
<tr>
<td>
<input type="text" class="form-control" name="v6" placeholder="Mæling 1" />
</td>
<td>
<input type="text" class="form-control" name="v9" placeholder="Mæling 1" />
</td>
<td>
<input type="text" class="form-control" name="v12" placeholder="Mæling 1" />
</td>
<td>
<input type="text" class="form-control" name="v15" placeholder="Mæling 1" />
</td>
</tr>
</table>
<button style="float:right;" class="btn btn-primary" id="calc"><span class="glyphicon glyphicon-cloud-download"></span> REIKNA</button>
基本上发生的是声明
$("#calc").click(function () {
console.log($(this).siblings()[0]);
});
返回表格,但是一旦我尝试在桌面上调用.children()
,它就说它不是一个函数。
答案 0 :(得分:3)
这是因为$(this).siblings()[0]
返回表的dom对象而不是jquery对象。您可以将表选择器作为参数传递给siblings方法以获取表的jquery对象:
$("#calc").click(function () {
console.log($(this).siblings('table').children());
});
答案 1 :(得分:2)
问题是$(this).siblings()[0]
返回一个dom元素引用,因此它无法访问jQuery方法。
因此,如果你想通过索引访问元素,那么使用.eq(index),它将返回对所述索引处元素的jQuery对象引用。
$(this).siblings().eq(0).children();
答案 2 :(得分:1)
您应该使用$($(this).siblings()[0]).children()
,因为您编写的方式是,您获取表的DOM对象,而不是jQuery对象。
答案 3 :(得分:0)
尝试使用.prev()
$(this).prev().children()
答案 4 :(得分:0)
$(this).siblings()[0]
是一个DOM元素。你可以用$(this).siblings().eq(0).children()
获得你想要的东西,或者使用DOM元素获取jQ对象,但它有点难看。
var domEl = $(this).siblings()[0];
var $jQEl = $(domEl);
var $children = $jQEl.children();
良好做法:使用$。
为jQ对象添加前缀