简单的ajax/尝试替换数字

simple ajax/trying to replace number

本文关键字:替换 数字 ajax 简单      更新时间:2023-09-26

我正试图用jquery替换span类中的内容。我知道我的jquery文件正在被读取,因为第一行有效。从本质上讲,jquery的第一行将一个新通知添加到通知列表中。第二行查询应该用新的@unseen_notifications.count替换网站上的现有号码。jquery文件的第二行不起作用。我该如何修复它?

jquery:

$('#user_notifications').prepend("<%= j render(@user_notifications) %>");
$('#red-count').html.replaceWith("<%= @unseen_notifications.count %>");

html:

<span class="red-count">
    <%= @unseen_notifications.count %>
</span>
<span class="red-count">
    <%= @unseen_notifications.count %>
</span>
$('.red-count').html("<%= @unseen_notifications.count %>");

如果使用类,请使用.而不是#

如果您想更改特定元素的html/内容,可以使用

$('YOUR ELEMENT SELECTOR').html('YOUR CONTENT')

如果你只是添加文本,你可以使用.text()也可以使用

 $('YOUR ELEMENT SELECTOR').text('YOUR TEXT')

您不能写入html.replaceWith。设置html如下

$('.red-count').html("<%= @unseen_notifications.count %>");

此外,红色计数是一个类,因此使用.而不是#

使用.text()而不是.html.ReplaceWith()

$('.red-count').text("<%= @unseen_notifications.count %>");