Ajax Reload iframe

时间:2010-08-24 11:37:06

标签: jquery ajax

如何使用jquery重新加载iframe?

<html>

<head>
<script type="text/javascript" src="jquery-1.4.2.js" > </script>


<script type="text/javascript">
//Reload Iframe Function    
</script>

</head>

<body>
    <iframe name="f1" id = "f1" src="http://www.google.com.pk/search?q=usa+current+time&ie=utf-8&oe=utf-8&aq=t&client=firefox-a&rlz=1R1GGLL_en" width=500px height=250px>
    </iframe>

<button onClick="abc()"> Reload </button>

</body>

</html>

2 个答案:

答案 0 :(得分:12)

根据您对Haim帖子的评论,您想要的内容可简化为:

$('#reload').click(function() {
    $('#f1').attr('src', $('#f1').attr('src'));
    return false;
});

或者

$('#reload').click(function() {
    $('#f1')[0].contentWindow.location.reload(true);
    return false;
});

但也许他的答案更适合你的需要。

<强>更新

我使用上面提供的方法汇总了一个工作示例。我在一个页面中有一个<iframe>,显示另一个打印时间的页面。

<iframe> HTML (time.html):

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>

    The time is now <span id="time"></span>.

    <script type="text/javascript">

        $(document).ready(function() {
            $('#time').html((new Date()).toString());
        });

    </script>
</body>

父页面HTML (test.html):

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>

    <iframe id="frame" src="file:///D:/Users/Cory/Desktop/time.html" width="500" height="250"></iframe>

    <button id="reload">Refresh</button>

    <script type="text/javascript">

        $(document).ready(function() {
            $('#reload').click(function() {
                $('#frame').attr('src', $('#frame').attr('src'));
                return false;
            });
        });

    </script>
</body>

在Firefox 3.6和IE 7中,这对我来说很合适。

答案 1 :(得分:2)

$("#reload").click(function() {
            jQuery.each($("iframe"), function() {
                $(this).attr({
                    src: $(this).attr("src")
                });
            });
            return false;
        });

每次点击带有一类reloadAds的链接并将iFrame的来源设置为自身时,这将遍历每个 iFrame