0

当用户单击按钮时,我想更新跨度标记中的内容。span 标签有一个名为的类fa_button_id_counter和一个id带有 post id 的类。它应该很简单,但我无法让它工作。有什么建议么?

查看文件:

<a class="like_button fa_button active" href="#<%= p.id %>" id="<%= p.id %>">FA</a>
<span class="fa_button_id_counter" id="<%= p.id %>">+<%= p.likes.count %></span>

.js 文件:

$ ->
  $(".like_button").click (e) ->
    e.preventDefault();
    id = $(this).attr('href')
    $(".fa_button_id_counter" + id).html "change to this"
4

1 回答 1

1

ids 不能以数字开头。必须从a-zA-Z.

要回答这个问题:

$(".fa_button_id_counter" + id).html('+'+(parseInt($(".fa_button_id_counter" + id).html(),10)+1))

parseInt接受一个字符串并解析出一个数字。如果"+8"给出,数字8将出来。但是,您需要注意始终提供第二个参数base, 设置为10。否则 javascript 将尝试为您猜测基数。如果输入为015,则输出为13,因为它猜测您的数字是以 8 为基数的。

编辑:谢谢mu is too short,并且vol7ron:如果您将文档类型更改为<!DOCTYPE html>.

于 2013-06-14T20:39:52.447 回答