日期:2014-05-16  浏览次数:20447 次

用js获取动态src
现有一个iframe和2个链接a1(百度首页),a2(谷歌首页)(两个链接都不在iframe中)。
开始时,当点击a1时将百度首页切换入iframe中(其地址可以不断变化,如百度中的搜索);然后点击a2时把iframe中新的地址保存到a1中,而把谷歌首页切换入iframe中;如果再点a1时iframe就会回到点击a2前的状态(百度中原来搜索页面)。。。
跪求js实现方法?

------解决方案--------------------
如果是不知道如何获取iframe里的链接,使用window.parent.window[X].location就可以了

如果是不知道怎么动态的给<a>替换这个src 可以使用getElementById("baidu").src = 上面这个就可以了
------解决方案--------------------
放2个iframe,用哪个就显示哪个,另一个就隐藏
------解决方案--------------------
HTML code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<head>
<title>无标题页</title>
<style>
#frame{
    widows:400px;
    height:200px;
    border:1px solid red;
}
</style>
<script src="jquery-1.3.2.js"></script>  
<script type="text/javascript">
$(document).ready(function(){
    $("#baidu").click(function(){
         var src =  $(this).attr("title");
         alert(src)
         $("#frame").attr("src",src);
    });
    $("#guge").click(function(){
         var src =  $(this).attr("title");
         alert(src)
         $("#frame").attr("src",src);
    });
});
</script>

</head>
<body>
<iframe id="frame">
   
</iframe>
<a href="#" id="baidu" title="http://www.baidu.com">百度</a>
<a href="#" id="guge" title="http://www.google.com">谷歌</a>
</body>
</html>

------解决方案--------------------
在lieri111的基础上稍微修改下:

HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<head>
<title>无标题页</title>
<style>
#frame{
    widows:400px;
    height:200px;
    border:1px solid red;
}
</style>
 
<script type="text/javascript">

function opensrc(obj){
    var src = obj.title;
    var iframeSrc = document.getElementById("frame").src;
    if("baidu" == this.id)
    {
        document.getElementById("guge").title = iframeSrc;
    }
    else
    {
        document.getElementById("baidu").title = iframeSrc;
    }
    document.getElementById("frame").src = src;
}
</script>

</head>
<body>
<iframe id="frame"></iframe>
<a href="#" id="baidu" onclick="opensrc(this)" title="http://www.baidu.com">百度</a>
<a href="#" id="guge" onclick="opensrc(this)" title="http://www.google.com">谷歌</a>
</body>
</html>