开发者

Trying to add an iframe using jQuery

开发者 https://www.devze.com 2023-02-08 06:11 出处:网络
I\'m trying to add an iframe using jquery this way below, but when I click the link it doesn\'t happen anything.

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

<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');

    &开发者_如何学Pythonlt;/script>

    </head>

    <body>

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

    </body>

</html>


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>


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


Change your function to:

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

That should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消