Javascript 当从 ajax 请求返回时,让 jQuery 在 DIV 中创建一个新行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6495045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting jQuery to make a new line inside a DIV when returning from an ajax request
提问by stackoverflow
<script>
$.ajax
({
url: "../getStatus/",
dataType: 'json',
success: function(json)
{
$('#ajaxLog').html(json.status+"<br/>");
}
});
</script>
//Need this section to make a new line when refreshed
<div id = ajaxLog style="width:50%"></div>
This needs to be an accumulative, returned message to the webpage. Each time the process starts/stop/suspends it will be displayed. The displayed message will not keep saying stopped/started/ etc. when remaining in a state - rather displayed each time these states change then it wont be redisplayed constantly -- NOT like jquery.append() does.
这需要是一个累积的、返回到网页的消息。每次进程启动/停止/暂停时都会显示。显示的消息在保持状态时不会一直说停止/启动/等 - 而是在每次这些状态改变时显示,然后它不会不断重新显示 - 不像 jquery.append() 那样。
Desired
想要的
started 10:48
stoped 10:50
Started 12:53
suspended 3:34
Started 3:40
NOT DESIRED
不想要
started 10:48
started 10:48
started 10:48
started 10:48
started 10:48
started 10:48
started 10:48
stoped 10:50
.
.
and so on
等等
(like append() would do)
(就像 append() 会做的那样)
回答by Nishchay Sharma
<script>
//Use this
last=null;
$.ajax
({
url: "../getStatus/",
dataType: 'json',
success: function(json)
{
//Replace this line
//$('#ajaxLog').html(json.status+"<br/>");
//with this:
if(last!=json.status)
{
$('#ajaxLog').append(json.status+"<br/>");
//or: $('#ajaxLog').append('<p>'+json.status+'</p>');
last=json.status;
}
}
});
</script>
//Need this section to make a new line when refreshed
<div id = ajaxLog style="width:50%"></div>
回答by Darin Dimitrov
There is no such thing as new linein HTML. You could use a <br/>
tag to achieve a new linesemantics:
HTML 中没有换行之类的东西。您可以使用<br/>
标签来实现新的行语义:
$('#ajaxLog').append(json.status + '<br/>');
Or put the contents into a div:
或者把内容放到一个div中:
$('#ajaxLog').append('<div>' + json.status + '</div>');
Also you should use the .append()
method instead of .html()
to avoid overwriting the contents of the ajaxLog
div each time this is invoked.
此外,您应该使用该.append()
方法而不是.html()
避免ajaxLog
每次调用时覆盖div的内容。
回答by rockerest
Add a line break in the HTML?
在 HTML 中添加换行符?
success: function(json)
{
$('#ajaxLog').html("<br />" + json.status+);
}
回答by Jim Ade
Unless I'm misreading this, you are calling the html() method which will replace the full contents of the div. Are you really trying to append the results (on a new line) to whatever is already inside the div?
除非我误读了这一点,否则您正在调用 html() 方法,该方法将替换 div 的全部内容。您是否真的想将结果(在新行上)附加到 div 中已经存在的任何内容?
My apologies if I'm not understanding the question correctly.
如果我没有正确理解问题,我深表歉意。
回答by Gjorgji Tashkovski
Shouldn't the ajax settings have an additional
ajax 设置不应该有额外的
data: ''
data: ''
so it looks like
所以它看起来像
url: '../getStatus/', data: '', dataType: 'json', success: ...
url: '../getStatus/', data: '', dataType: 'json', success: ...
? I'm pretty much sure I've read this somewhere.
? 我很确定我在某处读过这篇文章。