jquery 查找下一个带有类的元素
我试图找到下一个具有“错误”类别的元素并碰壁。
在查看 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>
我试图在相关元素之后找到 span 或 div 或其他任何内容,如上面的按钮。
所以禁用的按钮行应该是“禁止覆盖此按钮已禁用”
我尝试过
$("button[disabled]").next(".error").text("此按钮已禁用") ;
无济于事。
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.
$("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.
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");
to no avail.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是您使用 next 遍历函数而不是 nextAll
当您使用 next 时,它只是查看“下一个”元素是“
下一个”,所有元素都会查看下一个的所有同级元素
The problem is that your using the next traversing function rather than nextAll
When you use next its just looking at the "next" element which is
Next all looks at all siblings that are next
试试这个:
希望有帮助。
司南。
Try this:
hope it helps.
Sinan.
next()
在这种情况下不起作用,因为它必须是同级才能起作用。在这种情况下,您需要:next()
won't work in this case because it has to be a sibling for that to work. In this case you need: