Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 简单ajax/jquery-尝试替换数字_Javascript_Jquery_Ruby On Rails_Ajax_Ajax Polling - Fatal编程技术网

Javascript 简单ajax/jquery-尝试替换数字

Javascript 简单ajax/jquery-尝试替换数字,javascript,jquery,ruby-on-rails,ajax,ajax-polling,Javascript,Jquery,Ruby On Rails,Ajax,Ajax Polling,我试图用jquery替换span类中的内容。我知道我的jquery文件正在被读取,因为第一行可以工作。实际上,jquery的第一行向通知列表中添加了一个新通知。第二行查询应使用新的@unseen_notifications.count替换站点上的现有编号。jquery文件的第二行不工作。我怎么修理它 jquery: $('#user_notifications').prepend("<%= j render(@user_notifications) %>"); $('#red-cou

我试图用jquery替换span类中的内容。我知道我的jquery文件正在被读取,因为第一行可以工作。实际上,jquery的第一行向通知列表中添加了一个新通知。第二行查询应使用新的@unseen_notifications.count替换站点上的现有编号。jquery文件的第二行不工作。我怎么修理它

jquery:

$('#user_notifications').prepend("<%= j render(@user_notifications) %>");
$('#red-count').html.replaceWith("<%= @unseen_notifications.count %>");
$(“#用户通知”)。前缀(“”);
$('#red count').html.replacetwith(“”);
html:


您无法编写
html.replaceWith
。如下所示设置html

$('.red-count').html("<%= @unseen_notifications.count %>");
$('.red count').html(“”);
另外,red count是一个类,因此使用
而不是
#

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

 $('YOUR ELEMENT SELECTOR').text('YOUR TEXT')
使用
.text()
而不是
.html.ReplaceWith()


$('.red count')。文本(“”)

在jQuery中是否执行一些特殊操作?你有参考例子吗?看起来你试图以一种不起作用的方式将服务器端代码与javascript混合在一起。。。您如何期望
@unseen_notifications.count
的服务器端值在脚本和html之间发生变化?其中看起来像ruby标记,但一旦页面呈现,它应该显示为正常文本我确信这就是问题所在,很高兴我没有错过jQuery的特殊功能:PI想知道它们的意思是什么“不行但是他们说第一个很好,谢谢,很有效。既然erb代码在页面加载后才执行,那么它是如何工作的?@Philip7899-很高兴它工作了。我不是一个喜欢ruby的人,但一般来说,代码隐藏在页面呈现之前的页面加载上运行,javascript在页面加载之后运行,所有DOM元素都准备好了(因此document.ready)。希望这有帮助。请投票选出所有有用的答案,并接受其中一个作为答案。
<span class="red-count">
    <%= @unseen_notifications.count %>
</span>

$('.red-count').html("<%= @unseen_notifications.count %>");
$('YOUR ELEMENT SELECTOR').html('YOUR CONTENT')
 $('YOUR ELEMENT SELECTOR').text('YOUR TEXT')