iframe中的脚本不起作用

时间:2013-07-23 11:29:26

标签: javascript iframe

我正在加载像这样的iframe

var fw = 600;
var fh = 300;
var left = (window.screen.width-fw)/2;
var top = (window.screen.height-fh)/2;
$.ajax('/app/ardeal/check_ardeals.htm', {
    data: {hookpoint: _hookpoint, lookupid: _lookupid},
    dataType: 'json',
    success: function(res){
        if(res.status == "success"){
            $('body').append('<div class="fancybox-overlay fancybox-overlay-fixed" style="width: auto; height: auto; display: block;">' +
                '<div id="ard-container" style="visibility: visible; position: absolute; margin: auto; z-index: 99999; ' +
                'overflow: hidden; background-color: #efefef; width: '+fw+'px; top: '+top+'px; left: '+
                left+'px; display: block; background-position: initial initial; background-repeat: initial initial;">' +
                '<iframe id="ardeal-frame" scrolling="no" style="width: 100%; height: 100%; background-color: transparent;"' +
                'src="/app/ardeal/get_ardeal.html?lookupid='+_lookupid+'&hookpoint='+_hookpoint+'" height=100%></iframe></div></div>');
        }
    },
    error: function(xhr){
        //do nothing. Ignore.
    }
});

这是我的iframe内容

    <!DOCTYPE html>
<html>
<body>
<a href="#" id="skip_reward">No. Thank You.</a>
    <script type="text/javascript">
        var jQuery = window.parent.jQuery;
        var $ = jQuery;
        window.onload = function(){
            window.parent.console.log("In iframe load");
        }
        $(document).ready(function(){
            window.parent.console.log("In iframe ready");
            $('#skip_reward').on('click', function(ev){
                ev.preventDefault();
                alert("Click within iframe");
            });
        })
    </script>
</body>
</html>

我在控制台中看到'iframe load'和'iframe ready'。 (奇怪的是两次。任何想法?)。但我的事件并没有受到约束。当我点击链接时,不会显示任何警报。如何从iframe中的脚本将事件绑定到iframe内的元素?

0 个答案:

没有答案