Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在jquery中访问同级_Javascript_Jquery - Fatal编程技术网

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()只返回列表中第一个元素的值