Javascript 使用jquery更改iframe src
我正在尝试更改我的iframe src并用以下代码重新加载iframeJavascript 使用jquery更改iframe src,javascript,jquery,iframe,Javascript,Jquery,Iframe,我正在尝试更改我的iframe src并用以下代码重新加载iframe <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> $('#changeframe').click(function () { $('#declinedframe').attr('src', 'http://stackoverflow.c
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$('#changeframe').click(function () {
$('#declinedframe').attr('src', 'http://stackoverflow.com');
});
</script>
<input type="button" id="changeframe" value="Change">
<iframe id="declinedframe" class="embed-responsive-item" src="http://forgezilla.com" height="585" width="100%" ></iframe>
$('#changeframe')。单击(函数(){
$('declinedframe').attr('src','http://stackoverflow.com');
});
当我点击按钮上的“更改”时,什么都没有发生。我做错了什么
$(函数(){…})代码>或将其移动到html下方,因为该按钮
脚本运行时不存在
$(函数(){
$('#changeframe')。单击(函数(){
$('declinedframe').attr('src','http://stackoverflow.com');
});
});代码>
顺序很重要
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="button" id="changeframe" value="Change">
<iframe id="declinedframe" class="embed-responsive-item" src="http://forgezilla.com" height="585" width="100%" ></iframe>
// Go Last since changeframe hasn't exist yet.
<script>
$('#changeframe').click(function () {
$('#declinedframe').attr('src', 'http://stackoverflow.com');
});
</script>
//因为changeframe还不存在,所以最后一步。
$('#changeframe')。单击(函数(){
$('declinedframe').attr('src','http://stackoverflow.com');
});
如果代码与示例中的代码相同,则需要将jQuery封装在$(function(){…})中代码>因为脚本运行时按钮不存在重新安排订单。脚本转到最后并重试:)-#您当前的顺序中尚不存在changeframe。可能在浏览器知道这些元素之前正在运行jQuery。。。因此,dom就绪函数?您似乎没有使用它…表单标记中有输入按钮吗?数字2不是真的。@Noobit我已更改为“may”,因为出于安全原因,有些浏览器不喜欢更改已加载帧的src。出于好奇,哪些浏览器阻止您更改页面上存在的iframe元素?这不傻,是吗?o_OYes-我没有测试IE11或Edge,但旧版IEs不允许对从不同域加载的iframe进行任何操作。从安全欺骗的角度来看是有意义的。这很有效。谢谢,可以使用文本链接而不是按钮吗?是的,因为它只是一次点击。我建议升级jQuery,因为版本1.11.1已经过时了:PHowever 1.11仍然与兼容IE@Noobit这个链接看起来怎么样?现在我有零钱,但没有用