Javascript 在jquery中访问同级
我想访问列的同级:Javascript 在jquery中访问同级,javascript,jquery,Javascript,Jquery,我想访问列的同级: <tr> <td>1</td> <td>2</td> <td>3</td> </tr> 1. 2. 3. 当我在3上单击时,如何访问1?您可以使用.sidesjquery函数 您可以使用.childrenjquery函数 或者您也可以使用.parent()和.children()方法来访问它 $(this).parent().childre
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
1.
2.
3.
当我在3上单击时,如何访问1?您可以使用
.sides
jquery函数
您可以使用
.children
jquery函数
或者您也可以使用.parent()和.children()方法来访问它
$(this).parent().children()
但更简单的版本是.jquery函数
http://api.jquery.com/children/或者您也可以使用.parent()和.children()方法来访问它
$(this).parent().children()
但更简单的版本是.jquery函数
http://api.jquery.com/children/如果我能很好地理解
var td = $('tr td'), len = td.length;
td.on('click', function() {
var i = (td.index(this) + 1) % len;
console.log(td.eq(i));
});
这会回来的
- 第二个
当您单击第一个td
td
- 第三个
点击第二个td
td
- 单击第三个
td
var td = $('tr td'), len = td.length;
td.on('click', function() {
var i = (td.index(this) + 1) % len;
console.log(td.eq(i));
});
这会回来的
- 第二个
当您单击第一个td
td
- 第三个
点击第二个td
td
- 单击第三个
td
$('td:last').on('click', function(){
$(this).siblings('td:first').css('color', 'red');
})
演示:您可以使用-
$('td:last').on('click', function(){
$(this).siblings('td:first').css('color', 'red');
})
Demo:$(this).parent().sibbines(“:first”).html()我尝试了这个,但在引用$(this)时不起作用,它引用了td元素,所以试试这个。您可以直接以$(this.sides(“:first”).html()访问,也可以使用$(this.parent().children(“:first”).html()访问。这对解决您的主要问题没有帮助,但我注意到您不需要第一个选择器,因为.html()只返回列表中第一个元素的值$(this.parent().sides(“:first”).html()。我尝试了这个,但不工作当您引用$(这个)它引用td元素时,请尝试这个。您可以直接以$(this.sides(“:first”).html()访问,或者使用$(this.parent().children(“:first”).html()访问。这对解决您的主要问题没有帮助,但我注意到您不需要第一个选择器,因为.html()只返回列表中第一个元素的值