gpt4 book ai didi

jquery - 使用 JQuery 检索 JSON 数据后 Html 表单元素消失

转载 作者:行者123 更新时间:2023-12-01 07:26:01 27 4
gpt4 key购买 nike

我知道这可能很简单,但我无法弄清楚。当我加载页面时,我会看到文本区域框和按钮闪烁,然后 json 数据显示在页面上,并将其替换。为什么其他元素都消失了?

<html>
<head>
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
</head>
<body>
<div id="wrapper">
<div id="chat">
<script type="text/javascript">
$(function(){
$.getJSON("process.php?jsoncallback=?", function(data){
for (var x = 0; x < data.length; x++) {
document.write(data[x].username);
document.write(': ');
document.write(data[x].comment);
document.write('<br/><br/>');
}
});
});
</script>
</div>

<div id="send">
<textarea id="send_text"></textarea>
<button>Send</button>
</div>


</div>
</body>
</html>

谢谢!

最佳答案

因为这就是 document.write 的作用。我猜您实际上想将您的内容附加到某个地方。您可以使用 jQuery htmltext设置元素内容的方法。

当您调用document.write时实际发生了什么? MDN article解释得很好:

Writing to a document that has already loaded without calling document.open() will automatically perform a document.open call.

以及 document.open 上的页面告诉我们这一点:

If a document exists in the target, this method clears it

关于jquery - 使用 JQuery 检索 JSON 数据后 Html 表单元素消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9550808/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com