使用 jQuery forEach 查找
原始点击发生的位置
我有一个
列表,其中可能会产生一些点击。我知道如何用这样的方式滚动它们:
$('p').each(function() {
});
但我不明白的是如何获取这些段落中某些项目的值。我正在此处查看 API 的属性: http://api.jquery.com/category/attributes/ 但不知怎的,我没有看到太多相关的东西。
这是我的
的典型内容,
<p class="half_text">0
<strong>
<a class="vote_up" style="color: #295B7B; font-weight:bold;" href="#" data-problem_id="48">Vote Up</a>
</strong>
| 0
<strong>
<a class="vote_down" style="color: #295B7B; font-weight:bold;" href="#" data-problem_id="48">Vote Down</a>
</strong>
</p>
我需要做的是增加或减少“half_text”内或 | 之后的 0。整个事情的中心人物。我可以将它们包装在 span 标记内,但是如何将 span 标记与特定
的 data-problem_id="48" 部分相匹配?
谢谢!!
I have a list of <p>
where some clicks may originate. I understand how to scroll through them with something like this:
$('p').each(function() {
});
But what I don't understand is how to get the values of some items inside those paragraphs. I am looking at the API here for attributes: http://api.jquery.com/category/attributes/ but somehow I see not much relevant stuff.
Here is a typical contents of my <p>
<p class="half_text">0
<strong>
<a class="vote_up" style="color: #295B7B; font-weight:bold;" href="#" data-problem_id="48">Vote Up</a>
</strong>
| 0
<strong>
<a class="vote_down" style="color: #295B7B; font-weight:bold;" href="#" data-problem_id="48">Vote Down</a>
</strong>
</p>
What I need to do is increment or decrement the 0 that is inside the "half_text" or right after the | character in the middle of the whole thing. I can wrap them inside the span tag, but how do I match up the span tag with the data-problem_id="48" part of that particular <p>
?
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我建议您将值包装成跨度以便于阅读:
然后,脚本更简单且更易于阅读:
I suggest you wrap your values into spans for a simpler reading :
And then, the script is more simple and more easy to read :
您根本不需要使用each 循环。如果您甚至要处理链接上的点击,您可以执行以下操作:
You don't need to use an each loop at all. If you're handling the click even on the links, you could do something like:
我将为您留下为 vote_down 做同样的事情。
I will leave for you doing the same thing for vote_down.
我想这就是你想要的 http://jsfiddle.net/7T4AK/
我添加了
spans
这样您就可以轻松访问需要递增的数字。这应该会让您走上正确的道路,弄清楚如何对否决票做同样的事情。
I think this is what you are after http://jsfiddle.net/7T4AK/
I added
spans
so you can easily access the number that needs to incremented.This should get you on the right track to figuring out how to do the same for the down vote.
从这里开始工作..
work on from here..
您应该将数字包装在
标记中,然后您可以执行以下操作:
演示:http://jsfiddle.net/gRLad/7/
You should wrap your numbers in
<span>
tags, and then you can do something like this:Demo: http://jsfiddle.net/gRLad/7/