jquery - 没有样式的 AJAX 请求 jQuery 链接

标签 jquery css ajax request href

我有这个代码。它获取请求数据并刷新显示在 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/

相关文章:

javascript - 如何将 jQuery 日期值转换为长值?

javascript - 隐藏/不显示元素接收鼠标悬停事件

css - 使用 CSS 放大和缩小鼠标点击

javascript - scrollIntoView 整个页面向下移动

ruby-on-rails - 为什么我的 ajax 调用呈现空白页面? ( ruby rails )

javascript - ckeditor 未在弹出对话框中加载通过 ajax 生成的元素?

javascript - 使用 FormData ajax 提交表单

jquery - 如何减去/排除两个 jQuery 查询?

javascript - 无法在回调时配置选项(jQuery 插件)

javascript - 为什么点击时值(value)相同?