jQuery jquery用类查找下一个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1793944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:08:54  来源:igfitidea点击:

jquery find next element with class

jquerydomtraversalnext

提问by Loony2nz

I'm trying to find the next element with a class of "error" and hitting a wall.

我试图找到下一个具有“错误”类的元素并撞墙。

In looking at the demo on jQuery's site, this should work, but doesn't.

在查看 jQuery 站点上的演示时,这应该有效,但没有。

$("button[disabled]").next().text("this button is disabled");

<div>
   <button disabled="disabled">First</button>
   <span>no overwrite</span>
   <span class="error"></span>
</div>

<div>
   <button>Second</button>
   <span></span>
</div>

<div>
   <button disabled="disabled">Third</button>
   <span>no overwrite</span>
   <span class="error"></span>
</div>

I'm trying to find the span or div or whatever after the element in question, like the button above.

我试图在有问题的元素之后找到 span 或 div 或任何东西,就像上面的按钮一样。

so the disabled button line should read, 'no overwrite this button is diabled'

所以禁用的按钮行应该是“没有覆盖这个按钮被禁用”

I've tried

我试过了

$("button[disabled]").next(".error").text("this button is disabled");

$("button[disabled]").next(".error").text("this button is disabled");

to no avail.

无济于事。

回答by PetersenDidIt

The problem is that your using the next traversing function rather than nextAll

问题是您使用下一个遍历函数而不是nextAll

$("button[disabled]").nextAll(".error").text("this button is disabled");

When you use next its just looking at the "next" element which is

当您使用 next 时,它只是查看“next”元素

<span>no overwrite</span>

Next all looks at all siblings that are next

Next all 查看接下来的所有兄弟姐妹

回答by Sinan

Try this:

尝试这个:

$("button[disabled=disabled]").parent().find("span.error").text("this button is disabled");

hope it helps. Sinan.

希望能帮助到你。思南。

回答by cletus

next()won't work in this case because it has to be a sibling for that to work. In this case you need:

next()在这种情况下不起作用,因为它必须是兄弟姐妹才能工作。在这种情况下,您需要:

$("button[disabled]").parent().nextAll()
  .find("span.error:first").text("this button is disabled");