用jQuery更改iframe源 [英] changing iframe source with jquery
本文介绍了用jQuery更改iframe源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经尝试了一段时间,并在SO上找到了类似问题的其他答案,但是当我尝试更改iframe的src属性时,它将在整个窗口中对其进行更新.这是我正在使用的以下代码,它们可以正常运行(无jquery):
I've been trying this for a bit now and have looked at other answers to similar questions on SO, but when I am trying to change the src attribute of an iframe, it updates it for the whole window. Here is the following code I am using that works correctly (no jquery):
<html>
<head>
<style type="text/css">
iframe#ifrm {
border:none;
padding:.5em;
margin:1.5em 0 1em;
width:100%;
height:100%;
}
</style>
<script src="./js/jquery-1.4.2.js" type="text/javascript"></script>
<script type="text/javascript">
// <![CDATA[
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// this is the function I'm trying to replace:
function loadIframe(iframeName, url) {
if ( window.frames[iframeName] ) {
window.frames[iframeName].location = url;
return false;
}
return true;
}
// ]]>
</script>
</head>
<body>
<ul>
<li><a href="http://www.google.com/" onclick="return loadIframe('ifrm', this.href)">Page 1</a> </li>
<li><a href="tabs.html" onclick="return loadIframe('ifrm', this.href)">Page 2</a></li>
</ul>
<div class="iframe">
<iframe name="ifrm" id="ifrm" src="tabs.html" frameborder="0">
Your browser doesn't support iframes.</iframe>
正如我所说,我已经尝试过
As I said, I've tried
$('#ifrm').attr('src', "http://www.google.com")
显示页面,但不在iframe中.我实际上只是在学习jquery,但是我无法弄清楚我的处境与其他类似的问题有何不同:
which displays the page, but not in the iframe. I'm really just learning jquery, but I can't figure out what's different about my situation than other similar questions like this:
谢谢.
推荐答案
应该可以.
这是一个可行的示例:
function loadIframe(iframeName, url) {
var $iframe = $('#' + iframeName);
if ( $iframe.length ) {
$iframe.attr('src',url);
return false;
}
return true;
}
这篇关于用jQuery更改iframe源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文