繁体   English   中英

函数jQuery内部的可变函数范围

[英]Variable Scope of function inside function jquery

我有这个jquery函数扩展,但redColorBtn的exec命令似乎不起作用。 如果我将var iframeDocument定义移到redColorBtn函数内部,那么它将起作用。 但是,我无法确定为什么会发生这种情况。 我以为是因为范围可变,但是iframe定义也不应在redColorBtn click函数之外起作用。 有人知道吗?

$.extend({
 handleToolbarEvents: function(iframeDocumentId) {
     var iframe          = document.getElementById(iframeDocumentId);
     var iframeDocument  = iframe.contentWindow.document;
     var redColorBtn     = document.getElementById('rte-redColorBtn-' + iframeDocumentId);

     $(redColorBtn).click(function() {
        var command = $(this).val();//foreColor
        iframeDocument.execCommand(command, false, "red");
     });

     $('.rte-button').click(function() {
        var command        = $(this).val();
        var iframeID       = $(this).closest('iframe[class="rte-iframe"]');
        var iframeDocument = iframe.contentWindow.document;
        iframeDocument.execCommand(command, null, null);
    });
 }
});

我曾经有一个相同的问题,即我添加了click事件,但是它不起作用。 这可能与您是否动态添加redColorBtn有关,这可能是您的问题。 您可以尝试redColorButton.bind('click', function(){}); 我不知道为什么,但是对我有用。

希望它能工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM