简体   繁体   English

加载DOM内容

[英]Load DOM content

Is there a way I can use Javascript to load a site's UI content in DOM? 有没有一种方法可以使用Javascript在DOM中加载网站的UI内容? For example 例如

<code>      
  <h1>Google</h1>
</code>

Retrieving "Google" (which has a h1 tag) in my Javascript code. 在我的Javascript代码中检索“ Google”(带有h1标签)。

Access DOM on your own page 在您自己的页面上访问DOM

To answer your exact question: 要回答您的确切问题:

var h1 = $('h1').html();

Yes, you might add jQuery to your site and use it's selector, like so: 是的,您可以将jQuery添加到您的站点并使用它的选择器,如下所示:

$( "#foo" )[ 0 ]; // = document.getElementById( "foo" )

Access DOM of other page 访问其他页面的DOM

This is not so easy, because of Cross Domain Policies. 由于跨域策略,这并不是那么容易。 Anyway, you will find out, if it is blocked. 无论如何,您将发现是否被阻止。 Try an Ajax query to get the website content, like so: 尝试使用Ajax查询来获取网站内容,如下所示:

$(document).ready(function() {
baseUrl = "http://www.somedomain.com/";
$.ajax({
    url: baseUrl,
    type: "get",
    dataType: "",
    success: function(data) {
        // play with data
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM