从JQuery创建iframe -属性不工作,为什么不呢?

Creating an iframe from JQuery - properties are not working, why not?

本文关键字:工作 为什么不 属性 JQuery 创建 iframe      更新时间:2023-09-26

你好,我正在创建一个iframe在我的javascript。然而,样式和加载事件似乎不起作用。我做错了什么?

var iframe = $("<iframe src='/Player/ShowPage/"+data.PageId+"'/>", {
                            style: "position:absolute; left:-100%",
                            load: function () {
                                var that = this;
                                $(this).animate({
                                    left: 0
                                }, 500);
                                $('iframe').not(this).animate({
                                    left: '-100%'
                                }, 500, function () {
                                    $('iframe').not(that).remove();
                                });
                            }
                        });
                        $('body').append(iframe);
我一定是错过了什么,我就是不知道是什么。iframe只是坐在那里——嘲弄我——在屏幕上…

你只需要把'src'移动到对象字面量中,一切都很好。

var iframe = $("<iframe>", {
    src: '/Player/ShowPage/' + data.PageId,
    style: "position:absolute; left:-100%",
    load: function () {
        var that = this;
        $(this).animate({
            left: 0
        }, 500);
        $('iframe').not(this).animate({
            left: '-100%'
        }, 500, function () {
            $('iframe').not(that).remove();
        });
    }
});
$('body').append(iframe);