访问jquery中的兄弟姐妹
我想访问列的兄弟:
1 2 3
当我点击3时,如何访问1?
您可以使用 –
$('td:last').on('click', function(){ $(this).siblings('td:first').css('color', 'red'); })
演示 : http : //jsfiddle.net/RK56q/
您可以使用.siblings
jquery函数
如果我很好理解;
var td = $('tr td'), len = td.length; td.on('click', function() { var i = (td.index(this) + 1) % len; console.log(td.eq(i)); });
这将回来
- 单击1st
td
时的第二个td
- 点击第二个
td
时的第3个td
- 点击第3个
td
时的第1个td
示例小提琴: http : //jsfiddle.net/BmYue/2/
或者您也可以使用.parent()和.children()方法来访问它。
$(this).parent().children()
但更简单的版本是.siblings jquery函数。
http://api.jquery.com/parent/ http://api.jquery.com/children/