如何使用 JQuery 设置同级元素的 HTML

发布于 2024-10-20 10:53:26 字数 224 浏览 1 评论 0原文

我有一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幽梦紫曦~ 2024-10-27 10:53:26

假设您的标记约为:

<div>
    <div class="upvote">vote up</div>
    <div class="votes">0</div>
    <div class="downvote">vote down</div>
</div>

您可以使用:

$('.upvote').siblings('.votes');

或者:

$('.upvote').parent().find('.votes');

参考文献:

  1. siblings()JS Fiddle
  2. parent(),
  3. find()

Edited in response to question from OP (in comments):

哦哦不,有多个同级“.votes”div 不是问题。问题是,给定页面上有多个提交,当您对其中一个投赞成票时,其余的也会得到赞成。我认为这就是为什么我需要使用 $this 或其他东西来仅使用选定的提交。有什么想法吗?

我不确定您所说的“给定页面上有多个提交”到底是什么意思,但我假设您的意思很简单,页面上有多个投票元素。如果是这种情况(并且我假设标记与我在本答案第一部分中的近似值一致),您可以使用:

$('.upvote').click(
    function(){
        $(this).siblings('.votes');
        // this is just a selector, it won't 'do' anything
        // other than select the element.
        });

Assuming your mark-up is approximately:

<div>
    <div class="upvote">vote up</div>
    <div class="votes">0</div>
    <div class="downvote">vote down</div>
</div>

You could use either:

$('.upvote').siblings('.votes');

Or:

$('.upvote').parent().find('.votes');

References:

  1. siblings(): JS Fiddle,
  2. parent(),
  3. find().


Edited in response to question from OP (in comments):

Ohhhh no it's not a problem with there being more than one sibling '.votes' div. The problem is that there are more than one submissions on a given page and when you upvote one the rest get upvoted as well. I think this is why I need to use $this or something to only use the selected submission. Any ideas?

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:

$('.upvote').click(
    function(){
        $(this).siblings('.votes');
        // this is just a selector, it won't 'do' anything
        // other than select the element.
        });
与之呼应 2024-10-27 10:53:26

有几种不同的方法可以访问 jQuery 中的特定同级。 查看它们。您可能需要 siblings()

There are several different ways to access specific siblings in jQuery. Have a look at them all. You probably want siblings().

在你怀里撒娇 2024-10-27 10:53:26
$('selector').siblings('.votes')
$('selector').siblings('.votes')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文