【问题标题】:Viewing HTML of external pages查看外部页面的 HTML
【发布时间】:2013-08-13 10:03:09
【问题描述】:

到目前为止,我所拥有的是:

<!DOCTYPE html>
<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
    </script>
    <script>
    $(document).ready(function(){
        $("button").click(function(){
            alert($("p").html());
        });
    });
    </script>
</head>
<body>
    <button>Return the content of the p element</button>
            <p>Test Text</p>
</body>
</html>

它的作用是显示我页面的内容。但是假设我有另一个看起来像这样的 html 文件:

<html>
<head>
</head>
<body>
    <p>This is text</p>
</body>
</html>

我将如何查看该页面的 html?

【问题讨论】:

标签: jquery html


【解决方案1】:

如果警报部分只是为了测试,而您想要做的是从第 2 页抓取一个元素并在第 1 页显示其内容,您可以使用$.load()

如果您在第 1 页上有:

<p id="page1paragraph"></p>
<script>
    $(document).ready(function() {
        $('#page1paragraph').load('page2url.htm #page2paragraph');
    });
</script>

然后第 2 页包含:

&lt;p id="page2paragraph"&gt;Text&lt;/p&gt;

它会进行 ajax 调用来获取页面内容,如果你在 URL 之后指定一个元素,它将只获取该元素的内容。此代码会将第 2 页上的段落标记的内容放入第 1 页的段落标记中。

jQuery 文档中有很多示例:http://api.jquery.com/load/

【讨论】:

    【解决方案2】:

    你可以给&lt;p&gt;元素一个ID:

    <p id="myP"></p>
    <script>
    $(document).ready(function() {
      $("button").click(function(){
          alert($("#myP").html());
        });
    });
    </script>
    

    【讨论】:

    • 假设我的文件名是 'test.html' 我会在这里放什么 &lt;p id="myP"&gt;&lt;/p&gt;
    • @DavisDude 只要你能控制 HTML,你就可以放任何你想要的东西。
    • 如果我没有呢?你会怎么做?
    猜你喜欢
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-18
    相关资源
    最近更新 更多