设置javascript变量的Ajax响应覆盖了所有html元素
有什么办法可以这样做吗?我的ajax设置了一个javascript变量,但当我尝试使用document.write显示它时,它覆盖了html元素。可以这样做吗?这样我就可以使用javascript变量而不必掩盖html元素?提前谢谢设置javascript变量的Ajax响应覆盖了所有html元素,javascript,ajax,variables,response,Javascript,Ajax,Variables,Response,有什么办法可以这样做吗?我的ajax设置了一个javascript变量,但当我尝试使用document.write显示它时,它覆盖了html元素。可以这样做吗?这样我就可以使用javascript变量而不必掩盖html元素?提前谢谢 <html> <head> <title>Log In</title> <!-- script tag for ajax jquery --> <link href='style.css' type
<html>
<head>
<title>Log In</title>
<!-- script tag for ajax jquery -->
<link href='style.css' type="text/css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function()
{
//Get Data ajax function
$.post(
'getData.php',
{
},
function(response)
{
$('#name').html($('#1' , response).html());
$('#sname').html($('#2' , response).html());
var x = $('#1' , response).html();
document.write(x);
})
return false;
})
</script>
</head>
<body>
<div id="name"></div>
<div id="sname"></div>
this is text that get covered up, as well as any other html elements
</body>
</html>
登录
$(文档).ready(函数()
{
//获取数据ajax函数
美元邮政(
“getData.php”,
{
},
功能(响应)
{
$('#name').html($('#1',response.html());
$('#sname').html($('#2',response.html());
var x=$('#1',response.html();
文件。编写(x);
})
返回false;
})
这是被掩盖的文本,以及任何其他html元素
文档。write
主要用于加载页面期间。响应事件时,可以通过createElement
、appendChild
等函数修改DOM;或者像innerHTML
这样的属性
例如,如果希望将x
的内容作为标记附加到div
中,而不是
document.write(x);
你应该这样做:
var div = document.createElement('div');
div.innerHTML = x;
document.body.appendChild(div);
如果您想找到DOM中已经存在的元素并对其进行修改,可以使用document.getElementById
来完成,如果该元素有ID,或者使用document.getElementsByTagName
来获得具有给定标记的元素列表(都是的一部分),或者(在大多数但不是所有当前浏览器上),来自的querySelector
和queryselectoral
更多阅读:
文档。write
主要用于加载页面期间。响应事件时,可以通过createElement
、appendChild
等函数修改DOM;或者像innerHTML
这样的属性
例如,如果希望将x
的内容作为标记附加到div
中,而不是
document.write(x);
你应该这样做:
var div = document.createElement('div');
div.innerHTML = x;
document.body.appendChild(div);
如果您想找到DOM中已经存在的元素并对其进行修改,可以使用document.getElementById
来完成,如果该元素有ID,或者使用document.getElementsByTagName
来获得具有给定标记的元素列表(都是的一部分),或者(在大多数但不是所有当前浏览器上),来自的querySelector
和queryselectoral
更多阅读:
$(document).ready(function()
{
//Get Data ajax function
$.post(
'getData.php',
{
},
function(response)
{
$('#name').html($('#1' , response).html());
$('#sname').html($('#2' , response).html());
var x = $('#1' , response).html();
// Use .append() instead of document.write()
$('body').append(x);
})
return false;
})
这样您就不会丢失任何现有数据。因此,既然您使用的是jQuery,就不应该使用document.write,因为其他人强调了这些原因。改用这个:
$(document).ready(function()
{
//Get Data ajax function
$.post(
'getData.php',
{
},
function(response)
{
$('#name').html($('#1' , response).html());
$('#sname').html($('#2' , response).html());
var x = $('#1' , response).html();
// Use .append() instead of document.write()
$('body').append(x);
})
return false;
})
这样,您就不会丢失任何现有数据。首先,您为什么要使用document.write(),其次,您为什么要尝试在中写入它?当jQuery运行它的
ready
方法时,页面已经被加载,HTML流被关闭。首先,为什么要使用document.write(),其次,为什么要尝试在中编写它?当jQuery运行其ready
方法时,页面已经被加载,HTML流被关闭。