jQuery 如何将url加载到div标签中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8335198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:22:13  来源:igfitidea点击:

how to load url into div tag

jquery

提问by vinanghinguyen

I have a <div id="content">want to load url: http://vnexpress.netcontent into my code:

<div id="content">想将 url: http://vnexpress.net内容加载到我的代码中:

<html>
    <head>
        <script type="text/javascript">
            $(document).ready(function(){
                $("#content").attr("src","http://vnexpress.net");
            })
        </script>
    </head>
    <body>
        <div id="content"></div>
    </body>
</html>

I don't want to use Iframe

我不想使用 iframe

回答by Carsten

Try the load() function.

试试load() 函数

$('#content').load("http://vnexpress.net");

Please not that for this to work, the URL to be loaded must either be on the same domain as the page that's calling it, or enable cross-origin HTTP requests ("Cross-Origin Resource Sharing", short CORS)on the server. This involves sending an additional HTTP header, in its most basic form:

请注意,要使其正常工作,要加载的 URL 必须与调用它的页面位于同一域中,或者在服务器上启用跨域 HTTP 请求(“跨域资源共享”,简称 CORS)。这涉及最基本的形式发送额外的 HTTP 标头

Access-Control-Allow-Origin:*

to allow requests from everywhere.

允许来自任何地方的请求。

回答by Sudhir Bastakoti

$(document).ready(function() {
 $('#content').load('your_url_here');
});

回答by circusbred

You need to use an iframe.

您需要使用一个iframe.

<html>
<head>
<script type="text/javascript">
    $(document).ready(function(){
    $("#content").attr("src","http://vnexpress.net");
})
</script>
</head>
<body>
<iframe id="content" src="about:blank"></iframe>
</body>
</html

回答by tmjam

<html>
<head>
<script type="text/javascript">
    $(document).ready(function(){
    $("#content").attr("src","http://vnexpress.net");
})
</script>
</head>
<body>
<iframe id="content"></div>
</body>
</html>

回答by k.i

Not using iframesputs you in a world of handling #document security issues with cross domain and links firing unexpected ways that was not intended for originally, do you really need bad Advertisements?

不使用 iframes会让你进入一个处理#document 安全问题的世界,跨域和链接触发原本不打算使用的意外方式,你真的需要糟糕的广告吗?

You can use jquery .load functionto send the page to whatever html element you want to target, assuming your not getting this from another domain.

您可以使用jquery .load 函数将页面发送到您想要定位的任何 html 元素,假设您没有从另一个域获取它。

You can use javascript .innerHTML valueto set and to rewrite the element with whatever you want, but if you add another file you might be writing against 2 documents in 1... like a in another

您可以使用javascript .innerHTML 值来设置和重写您想要的任何元素,但是如果您添加另一个文件,您可能会针对 1 中的 2 个文档编写......就像另一个

iframes are old, another way we can add "src" into the html alone without any use for javascript.But it's old, prehistoric, and just plain OLD! Frameset makes it worse because I can put #document in those to handle multiple html files. An Old way people created navigation menu's Long and before people had FLIP phones.

iframe 很旧,另一种方法是我们可以单独将“src”添加到 html 中,而无需使用任何 javascript。但它是古老的,史前的,而且只是简单的旧!Frameset 使情况变得更糟,因为我可以将 #document 放入其中以处理多个 html 文件。人们创建导航菜单的古老方式 早在人们拥有 FLIP 手机之前。

1.) Yes you will have to work in Javascript if you do NOT want to use an Iframe.

1.) 是的,如果您不想使用 Iframe,则必须使用 Javascript。

2.) There is a good hack in which you can set the domain to equal each other without having to set server stuff around. Means you will have to have edit capabilities of the documents.

2.) 有一个很好的技巧,您可以将域设置为彼此相等,而无需设置服务器内容。意味着您必须具有文档的编辑功能。

3.) javascript window.document is limited to the iframe itself and can NOT go above the iframe if you want to grab something through the DOM itself. Because it treats it like a separate tab, it also defines it in another document object model.

3.) javascript window.document 仅限于 iframe 本身,如果您想通过 DOM 本身抓取某些内容,则不能超过 iframe。因为它将它视为一个单独的选项卡,所以它还在另一个文档对象模型中定义它。