JQuery在div中加载内容
我在使用JQuery加载html时遇到困难。以下是从另一个帖子中借来的。我无法在Chrome中工作。有人能帮我整理一下代码,这样它就可以针对所有浏览器进行优化吗?谢谢JQuery在div中加载内容,jquery,Jquery,我在使用JQuery加载html时遇到困难。以下是从另一个帖子中借来的。我无法在Chrome中工作。有人能帮我整理一下代码,这样它就可以针对所有浏览器进行优化吗?谢谢 $(document).ready(function () { $("#selectchoice").change(function () { var selectedOption = $('#selectchoice :selected').val(); $containerDiv =
$(document).ready(function () {
$("#selectchoice").change(function () {
var selectedOption = $('#selectchoice :selected').val();
$containerDiv = $('#get_content');
$containerDiv.html("");
switch (selectedOption) {
case "1":
$containerDiv.html("http://www.google.com/index.html");
break;
case "2":
$containerDiv.load("http://www.yahoo.com/index.html");
break;
case "3":
$containerDiv.load("http://www.bing.com/index.html");
break;
default:
$containerDiv.load("");
break;
}
return true;
});
});
<select id="selectchoice">
<option>Select a choice</option>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
$(文档).ready(函数(){
$(“#选择选项”)。更改(函数(){
var selectedOption=$('#selectchoice:selected').val();
$containerDiv=$(“#获取内容”);
$containerDiv.html(“”);
开关(选择选项){
案例“1”:
$containerDiv.html(“http://www.google.com/index.html");
打破
案例“2”:
$containerDiv.装货(“http://www.yahoo.com/index.html");
打破
案例“3”:
$containerDiv.装货(“http://www.bing.com/index.html");
打破
违约:
$containerDiv.装载(“”);
打破
}
返回true;
});
});
选择一个选项
1.
2.
3.
不能使用XHR加载远程页面。
尝试使用iframe对象,如下所示:
// html
<iframe src="" id="remotesite"></iframe>
// and the script
document.getElementById('remotesite').load = function() {
// i'm loaded ;)
};
document.getElementById('remotesite').src = 'http://www.google.fr';
//html
//剧本呢
document.getElementById('remotesite')。load=function(){
//我很忙;)
};
document.getElementById('remotesite')。src='10〕http://www.google.fr';
小提琴;)
您可以尝试以下操作:
// Add iframe in div
<div id = 'get_contentdiv' style="height:400px;width:500px"><iframe width=400px height=400px src="" id="get_content"></iframe></div>
这里是演示:以您所使用的方式向第三方域发出AJAX请求将不起作用,原因是。您可以在我创建的JSFIDLE中添加更多内容,以便看到实际问题。我的目的是通过下拉菜单打开包含nvd3.js内容的各个页面。如果JQuery不能做到这一点,那么我还有什么其他选择?请原谅,但我不太确定如何完全实现此解决方案。是否可以替换默认设置,使其加载初始页面,而不是空白iframe?谢谢!很好地工作。是否可以替换默认值,让它加载初始页面,而不是空白iframe?
$(document).ready(function () {
$("#selectchoice").change(function () {
var selectedOption = $('#selectchoice :selected').val().trim();
$containerDiv = $('#get_content');
$containerDiv.html("");
switch (selectedOption) {
case "1":
$containerDiv.attr("src","http://www.google.com");
break;
case "2":
$containerDiv.attr("src","http://www.yahoo.com");
break;
case "3":
$containerDiv.attr("src","http://www.bing.com");
break;
default:
$containerDiv.html("");
break;
}
return true;
});
});