【问题标题】:Get inner html of an element from another web page?从另一个网页获取元素的内部 html?
【发布时间】:2013-06-26 05:01:48
【问题描述】:

如何使用 JavaScript 从另一个网页获取元素的innerHTML

例子:

<script>
  $.get(url, function(response) {
     var $response = $(response); // Code not working from this line
     alert($response.find('.result').html()); // get innhtml of element by class name from response
  });
</script>

【问题讨论】:

  • 提醒您的回复,看看您得到了什么

标签: jquery html innerhtml


【解决方案1】:

这取决于您的响应是否包含 html,这是否可行

    <script>
      $.get(url, function(response) {
         $('yourelement_selector',response).html();
      }
    </script>

【讨论】:

    【解决方案2】:

    问题一:&lt;scrip&gt; 必须是&lt;script&gt;

    问题 2:您还没有完成对 get 的函数调用,请将 ); 添加到脚本末尾

    除此之外,给定合适的输入,脚本works as desired

    然而,jQuery(和/或它调用的底层浏览器方法)的变幻莫测意味着如果您尝试匹配的元素是您正在加载的文档中 body 元素的 children ,那么它们将是 jQuery 对象中的 顶级 元素,因此您不会使用 find 找到它们(因为它们不是后代)。如果你想匹配这样的元素,请改用filter

    【讨论】:

    • @JAslu — 该网站未授予我网站上的 JavaScript 从中读取内容的权限。
    【解决方案3】:

    JQuery 的 .load() 会做到这一点:

    $('#result').load('ajax/test.html #container');
    

    更多信息在这里:http://api.jquery.com/load/

    【讨论】:

      【解决方案4】:

      您可以使用下面的 post 方法示例从其他站点获取数据并将该数据放入您的站点 div/span 等数据容器中。

      $.post("http://www.otherweburl.com",function (data) {
              document.getElementById("htmlContainerDivOrSpanETC").innerHTML=data;
      });
      

      【讨论】:

      • 问题是问如何从另一个页面获取一个元素,而不是整个页面的HTML。它没有说明使用帖子,也没有说明网站是外部的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-21
      • 1970-01-01
      • 2011-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多