尝试使用 jQuery 添加 iframe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4873291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:05:39  来源:igfitidea点击:

Trying to add an iframe using jQuery

jqueryiframe

提问by ziiweb

I'm trying to add an iframe using jquery this way below, but when I click the link it doesn't happen anything.

我正在尝试使用 jquery 在下面以这种方式添加 iframe,但是当我单击链接时,它什么也没发生。

<head>

    <script type="text/javascript"
            src='//ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js'></script>

    <script type="text/javascript">

        function fun(){
            $('<iframe />');  // Create an iframe element
            $('<iframe />', {
                name: 'frame1',
                id: 'frame1',
                src: 'http://www.programmingfacts.com'
            }).appendTo('body');

    </script>

    </head>

    <body>

        <a href="#" onclick="fun()">clica</a>

    </body>

</html>

回答by benhowdle89

My guess is that you dont close off your function fun() closing bracket? }

我的猜测是你没有关闭你的函数 fun() 结束括号?}

回答by Aaron Newton

Try this:

尝试这个:

<html>
    <head>
        <script type="text/javascript" src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js'></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $(".submit").click(function()
                {
                    $('<iframe />');  // Create an iframe element
                    $('<iframe />', {
                        name: 'frame1',
                        id: 'frame1',
                        src: 'http://www.programmingfacts.com'
                    }).appendTo('body');
                });
            });
        </script>
    </head>
    <body>
        <a href="#" class="submit">clica</a>
    </body>
</html>

回答by Barrie Reader

Change your function to:

将您的功能更改为:

function letsHaveFun(){
  $('body').append('<iframe src="http://www.programmingfacts.com" name="frame1" id="frame1"></iframe>');
}

That should work.

那应该工作。