如何使用 JQuery 设置同级元素的 HTML
我有一个包含 3 个 DIV 元素的 DIV 块:赞成票、投票票和反对票。当我单击“赞成票”或“反对票”并在 JQuery 代码块中调用“parent.html(html)”时,它会将投票计数覆盖在“赞成票”或“反对票”图标上。我将使用哪个 JQuery 调用来访问 DIV 类“投票”?我会找到父元素的父元素,然后搜索“votes”类吗?有什么方法可以通过名称查找元素的兄弟元素吗?抱歉这个新手问题,但我对 JQuery 还很陌生!
I have a DIV block with 3 DIV elements: upvote, votes, and downvote. When I click on upvote or downvote and call parent.html(html) within a JQuery block of code it overlays the vote count over the upvote or downvote icon. Which JQuery call would I use to gain access to the DIV class, "votes"? Would I find the parent of the parent element and then search for the "votes" class? Are there any ways to find an element's siblings by name? Sorry for the newbie question but I am still very new to JQuery!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您的标记约为:
您可以使用:
或者:
参考文献:
siblings()
:JS Fiddle,parent()
,find()
。Edited in response to question from OP (in comments):
我不确定您所说的“给定页面上有多个提交”到底是什么意思,但我假设您的意思很简单,页面上有多个投票元素。如果是这种情况(并且我假设标记与我在本答案第一部分中的近似值一致),您可以使用:
Assuming your mark-up is approximately:
You could use either:
Or:
References:
siblings()
: JS Fiddle,parent()
,find()
.Edited in response to question from OP (in comments):
I'm not sure what, exactly, you mean by 'more than one submissions on a given page,' but I'm assuming you mean, simply, that there are multiple voting elements on the page. If that's the case (and I'm assuming the mark-up is consistently as I approximated in the first part of this answer), you can use:
有几种不同的方法可以访问 jQuery 中的特定同级。 查看它们。您可能需要
siblings()
。There are several different ways to access specific siblings in jQuery. Have a look at them all. You probably want
siblings()
.