Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.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_Html - Fatal编程技术网

Javascript 接下来,最近的选择器无法使用Jquery

Javascript 接下来,最近的选择器无法使用Jquery,javascript,jquery,html,Javascript,Jquery,Html,在下面的html中,jquery下一个和最近的选择器不工作 当我尝试alert$.class1.next.class2.text时,alert$.class1.closest.class2.text两个警报均为空。我需要发出“是”警报 代码中的错误是什么。下一个错误不是class.class2的元素。使用下一个: 警报$.class1.next.next.class2.text; 你好 对 next只查看下一个直接兄弟姐妹。你想要的是: console.log$.class1.nextAl

在下面的html中,jquery下一个和最近的选择器不工作

当我尝试alert$.class1.next.class2.text时,alert$.class1.closest.class2.text两个警报均为空。我需要发出“是”警报

代码中的错误是什么。

下一个错误不是class.class2的元素。使用下一个:

警报$.class1.next.next.class2.text; 你好 对 next只查看下一个直接兄弟姐妹。你想要的是:

console.log$.class1.nextAll.class2.text; 你好 对
class1和class2的div在dom中处于同一级别。因此,您可以查看class1的兄弟姐妹并搜索class2

$(".class1").siblings('.class2').text()
// => "Yes"
或者您可以使用两者的父元素来查找其下的类2

$(".class1").parent().find('.class2').text()
// => "Yes"
作为紧跟在兄弟姐妹之后的下一个唯一目标,因此您的代码不起作用

您可以获取.parent,然后使用.find/.children

console.log$.class1.parent'div'.find.class2.text; 你好 对
$(".class1").parent().find('.class2').text()
// => "Yes"
$(".class1").parent('div').find(".class2").text()