小编Ty *_*obs的帖子

使用 Javascript、JQuery 动态更改 iFrame Src

过去几个小时我一直在尝试更改 iFrame 的 src。这似乎是一个简单的任务,但由于某种原因,经过数百次尝试并阅读了堆栈上的许多其他问题和答案后,我仍然发现自己无法更改 iFrame 的 src。正如您从下面的代码中看到的,我已经尝试过很多次了。我究竟做错了什么?

    <body>

       <p><iframe src="http://localhost/ok.html" name="myFrame" width="500" marginwidth="0"     height="500" marginheight="0" align="middle" scrolling="auto"></iframe>


<script src="http://code.jquery.com/jquery-latest.js"></script>
    <script>
    function loadPages(){
        var loc = "http://localhost/morningmarketweb/index.html";

        //var myIframe = document.getElementById("myFrame");

        //window.frames[myIframe].location = "http://localhost/morningmarketweb/index.html";

        // $(myIframe).load('http://localhost/morningmarketweb/index.html');

        // document.getElementId('myFrame').src="google.com";

       // var myIframe = document.getElementById('myFrame');
       // myIframe.src=loc;


        //$('#myFrame').attr('src', loc);

        document.getElementById('myFrame').setAttribute('src', loc);


    }
    </script>
    </body>
Run Code Online (Sandbox Code Playgroud)

javascript iframe jquery

0
推荐指数
1
解决办法
5万
查看次数

标签 统计

iframe ×1

javascript ×1

jquery ×1