我有这个代码。它获取请求数据并刷新显示在 DIV 中。
为什么它可以在 DIV 中显示请求的数据,如“简单文本”,但不能显示例如这一行:
<a href="#" data-role="button">Button</a>
应用/呈现 CSS 和 jQuery.mobile 按钮样式?我错过了什么吗?
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css">
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>
<script>
function GetData()
{
nocache = "&nocache=" + Math.random() * 1000000;
var request = new XMLHttpRequest();
request.onreadystatechange = function()
{
if (this.readyState == 4) {
if (this.status == 200) {
if (this.responseText != null) {
document.getElementById("buttons").innerHTML = this.responseText;
}
}
}
}
request.open("GET", "state" + nocache, true);
request.send(null);
setTimeout('GetData()', 1000);
}
</script>
</head>
<body onload="GetData()">
<div id="buttons" data-role="content">
</div>
</body>
</html>
最佳答案
替换这个 document.getElementById("buttons").innerHTML = this.responseText;
与
$("#buttons").html(this.responseText);
我就是这么做的。
此外,由于您使用的是 jQuery,因此您可以用 $(function(){ //your code here });
替换大部分用于检查就绪状态等的代码。
然后您也可以取消 <body onload="GetDate()">
并简单地拥有 <body>
.
关于jquery - 没有样式的 AJAX 请求 jQuery 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15670956/