使用 jQuery 识别特定元素
我正在实现一个投票系统,我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您需要在模板中的某处添加
postid
。只要每次都能检索到,在哪里都没关系。例子:
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:
使用
data-
属性将 postid 存储在元素上,例如然后在您的点击事件上:
Store the postid on the element using an
data-
attribute, e.g.Then on your click event:
给每个link元素添加id属性:
在JS代码中引用该元素:
add an id attribute to each link element:
reference the element in the JS code:
您可以将 id 存储在周围的 p 标记中
,并使用例如 jQuery 从那里提取它
you could store the id in the surrounding p tag
and extract it from there using e.g. jQuery
为每个链接指定一个与帖子 ID 匹配的 ID,然后使用
For the post ID
Give each link an ID that matchs the post ID, and then use
For the post ID
我认为您会想使用 html5 数据属性进行研究。
例如,你的 html 可能会变成:
虽然你的 JS 会读为:
虽然这不是最好的可用文档,但这应该
I think you'll want to investigate using html5 data attributes.
For instance, your html may become:
While your JS would read:
While this isn't the best doc available, this should
将 id 与特定类一起添加到帖子的包装中,即。
然后
Add an id to the wrapper of the post along with a specific class ie.
then