使用 jQuery 识别特定元素

发布于 2024-11-16 02:13:54 字数 1018 浏览 1 评论 0原文

我正在实现一个投票系统,我想使用 jQuery。我在单个页面上有投票的工作代码,但问题是我也希望能够从我的主页投票,并且我不知道如何确定投票的目的是哪个帖子。

我的 HTML 看起来像这样:(

<p>
post content
<span><a href="#" class="voteUp">I approve this message <span>${post.upvotes}</span></a></span>
</p>

<p>
post #2 content
<span><a href="#" class="voteUp">I approve this message <span>${post.upvotes}</span></a></span>
</p>

请忽略多个 span 标签)

这是我现在拥有的 JS 代码:

$(".voteUp").click(function(){
    $.post(voteAction({postid: '5', type: 'up'}), function(data){
        $("")
    });
});

那么我如何识别点击是针对哪个帖子的(并用所选的 id 替换硬编码的 postid?)我可以在 html 中的某处添加带有 ${post.id} 的 postid,但我不知道如何准确使用它。我无法为每个帖子生成自定义 jQuery .click 函数,对吧?

编辑:

知道如何更新跨度标签内容吗?我尝试了这个,但它不起作用:

$.post(voteAction({postid: this.id, type: 'up'}), function(data){
    $(".voteUp a span").html(data);
});

I'm implementing a vote system and I want to use jQuery. I have the working code for the vote on a single page, but the problem is that I want to be able to vote from my main page as well and I don't know how to identify for which post the vote is intended.

My HTML looks like this:

<p>
post content
<span><a href="#" class="voteUp">I approve this message <span>${post.upvotes}</span></a></span>
</p>

<p>
post #2 content
<span><a href="#" class="voteUp">I approve this message <span>${post.upvotes}</span></a></span>
</p>

(please ignore the multiple span tags)

This is the JS code I have right now:

$(".voteUp").click(function(){
    $.post(voteAction({postid: '5', type: 'up'}), function(data){
        $("")
    });
});

So how do I identify for which post the click is meant (and replace the hardcoded postid with the chosen id?) I could add the postid with ${post.id} somewhere in the html, but I don't see how to exactly use it. I can't afford to generate a custom jQuery .click function for each post, right?

EDIT:

Any idea how I update the span tags content afterwards? I tried this but it's not working:

$.post(voteAction({postid: this.id, type: 'up'}), function(data){
    $(".voteUp a span").html(data);
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

冷默言语 2024-11-23 02:13:54

您需要在模板中的某处添加 postid。只要每次都能检索到,在哪里都没关系。

例子:

<p>
post #2 content
<span><a href="#" class="voteUp" rel="${post.id}">I approve this message <span>${post.upvotes}</span></a></span>
</p>

$(".voteUp").click(function(){
    $.post(voteAction({postid: $(this).attr('rel'), type: 'up'}), function(data){
        $("")
    });
});

You need to add the postid somewhere in your template. It doesn't matter where as long as you can retrieve it every time.

Example:

<p>
post #2 content
<span><a href="#" class="voteUp" rel="${post.id}">I approve this message <span>${post.upvotes}</span></a></span>
</p>

$(".voteUp").click(function(){
    $.post(voteAction({postid: $(this).attr('rel'), type: 'up'}), function(data){
        $("")
    });
});
灼疼热情 2024-11-23 02:13:54

使用 data- 属性将 postid 存储在元素上,例如

<p data-postid="5">
    <a href="#" class="voteUp">
</p>

然后在您的点击事件上:

$(".voteUp").click(function(){
    var $postBlock = $(this).closest("p"); // gets the post block

    var postid = $postBlock.data("postid"); // 5

    ...
}

Store the postid on the element using an data- attribute, e.g.

<p data-postid="5">
    <a href="#" class="voteUp">
</p>

Then on your click event:

$(".voteUp").click(function(){
    var $postBlock = $(this).closest("p"); // gets the post block

    var postid = $postBlock.data("postid"); // 5

    ...
}
万劫不复 2024-11-23 02:13:54

给每个link元素添加id属性:

<p>
post content
<span><a href="#" class="voteUp" id="1">I approve this message <span>${post.upvotes}</span></a></span>
</p>

<p>
post #2 content
<span><a href="#" class="voteUp" id="2">I approve this message <span>${post.upvotes}</span></a></span>
</p>

在JS代码中引用该元素:

$(".voteUp").click(function(){
    $.post(voteAction({postid: this.id, type: 'up'}), function(data){
        $("")
    });
});

add an id attribute to each link element:

<p>
post content
<span><a href="#" class="voteUp" id="1">I approve this message <span>${post.upvotes}</span></a></span>
</p>

<p>
post #2 content
<span><a href="#" class="voteUp" id="2">I approve this message <span>${post.upvotes}</span></a></span>
</p>

reference the element in the JS code:

$(".voteUp").click(function(){
    $.post(voteAction({postid: this.id, type: 'up'}), function(data){
        $("")
    });
});
酒几许 2024-11-23 02:13:54

您可以将 id 存储在周围的 p 标记中

<p id="post-234">

</p>

,并使用例如 jQuery 从那里提取它

$(this).closest("p").attr("id").split("-")[1] --> the ID

you could store the id in the surrounding p tag

<p id="post-234">

</p>

and extract it from there using e.g. jQuery

$(this).closest("p").attr("id").split("-")[1] --> the ID
孤城病女 2024-11-23 02:13:54

为每个链接指定一个与帖子 ID 匹配的 ID,然后使用

$(this).attr('id')

For the post ID

Give each link an ID that matchs the post ID, and then use

$(this).attr('id')

For the post ID

無處可尋 2024-11-23 02:13:54

我认为您会想使用 html5 数据属性进行研究。

例如,你的 html 可能会变成:

<a href="#" class="voteUp" data-post-id="5">I approve....</a>

虽然你的 JS 会读为:

$(".voteUp").click(function(){
    $.post(voteAction({postid: $(this).data("post-id"), type: 'up'}), function(data){
        $("")
    });
});

虽然这不是最好的可用文档,但这应该

I think you'll want to investigate using html5 data attributes.

For instance, your html may become:

<a href="#" class="voteUp" data-post-id="5">I approve....</a>

While your JS would read:

$(".voteUp").click(function(){
    $.post(voteAction({postid: $(this).data("post-id"), type: 'up'}), function(data){
        $("")
    });
});

While this isn't the best doc available, this should

戏剧牡丹亭 2024-11-23 02:13:54

将 id 与特定类一起添加到帖子的包装中,即。

<div class="post-votable" id="post-5">
<p>
post content
<span><a href="#" class="voteUp">I approve this message <span>${post.upvotes}</span></a></span>
</p>

然后

$(".voteUp").click(function(){
    $.post(voteAction({postid: $('this').closest('.post-votable')[0].id.split('-')[1], type: 'up'}), function(data){
        $("")
    });
});

Add an id to the wrapper of the post along with a specific class ie.

<div class="post-votable" id="post-5">
<p>
post content
<span><a href="#" class="voteUp">I approve this message <span>${post.upvotes}</span></a></span>
</p>

then

$(".voteUp").click(function(){
    $.post(voteAction({postid: $('this').closest('.post-votable')[0].id.split('-')[1], type: 'up'}), function(data){
        $("")
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文