【问题标题】:.load function to call specific Class.load 函数调用特定的类
【发布时间】:2015-01-21 00:33:02
【问题描述】:

我正在尝试在一页中获取几个新闻网站的最新新闻部分。 我发现 JQuery 中的 .load 函数具有这种能力。但是怎么做?

 <html>
  <head>
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script>$("#today").load(http://www.today.mn + '.lastnews-wrapper'), function(data){};</script>
  </head>
  <body>
    <div id="today" style="width:400px; height:700px border:1px solid; border:#FF2;"></div>  
  </body>
</html>

到目前为止,这就是我所做的。但屏幕仍然是空白的

【问题讨论】:

  • .load() 从服务器而不是互联网加载内容。我认为您所追求的是 Ajax 并在新数据可用时重新加载。
  • 语法错误:您必须将 url 用引号括起来并删除 , function(data){} 部分... 概念性错误:您将无法加载内容很可能是由于 CORS

标签: jquery class load


【解决方案1】:

语法错误(load() 语法、css、url、加载回调等),可能还有来源限制。

试试这个 - 假设容器:.lastnews-wrapper 实际上保存了您所需要的数据。

<html>
    <head>
        <script src="//code.jquery.com/jquery-1.10.2.js"></script>
        <script>
        $(function(){ 
            $("#today").load("http://www.today.mn .lastnews-wrapper",function(responseTxt,statusTxt,xhr){
               if(statusTxt=="success")
                   console.log("Loaded successfully!");
               if(statusTxt=="error")
                   console.log("Error: "+xhr.status+": "+xhr.statusText);
            });
        });
        </script>
    </head>
<body>
    <div id="today" style="width:400px; height:700px; border:1px solid #FF2;"></div>
</body>
</html>

查看控制台,您应该会看到一条失败(或成功)消息,其中包含原因。

享受吧。

【讨论】:

    猜你喜欢
    • 2022-01-19
    • 2019-04-17
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    相关资源
    最近更新 更多