javascript 将 URL 加载到 div 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27576298/
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
Load URL into a div
提问by paisapimp
this script load content using an IFRAME is there any way to just load the content from the page but not as iframe?
这个脚本使用 IFRAME 加载内容有什么方法可以只从页面加载内容而不是 iframe 吗?
something like
就像是
$("#content2").load($(this).attr("page"));
But i can't seem to make it work on the above script.
但我似乎无法让它在上面的脚本上工作。
<script type="text/javascript">
function loadURL(u) {
document.getElementById("web-panel").innerHTML = '<iframe src="' + u + '"
width="100%" height="100%" border="0"></iframe>';}</script>
<select name="mydropdown" id="url" onchange="loadURL(this.value);">
<option value="http://123.com/opt.php?3">opt3</option>
<option value="http://123.com/opt.php?2">opt2</option>
<option value="http://123.com/opt.php?1">opt1</option>
</select>
<div id="web-panel"></div>
采纳答案by Eric
To load content of an URL into a div without using the IFrame, you could try using jQuery Ajax approach: (the first line is important, as it includes the jQuery framework)
要在不使用 IFrame 的情况下将 URL 的内容加载到 div 中,您可以尝试使用 jQuery Ajax 方法:(第一行很重要,因为它包含 jQuery 框架)
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script type="text/javascript">
function loadURL(u) {
$.ajax({
url: u,
cache: false
}).done(function(data) {
$("#web-panel").html(data);
});
}
</script>
<select name="mydropdown" id="url" onchange="loadURL(this.value);">
<option value="http://123.com/opt.php?3">opt3</option>
<option value="http://123.com/opt.php?2">opt2</option>
<option value="http://123.com/opt.php?1">opt1</option>
</select>
<div id="web-panel"></div>
回答by user2266705
Looks like you have JavaScript syntax error.
看起来您有 JavaScript 语法错误。
Change the JS part to look like this:
将 JS 部分更改为如下所示:
function loadURL(u) {
document.getElementById("web-panel").innerHTML = '<iframe src="' + u +
'" width="100%" height="100%" border="0"></iframe>';
}