我有一个自定义jQuery函数,我需要结合两种IFRAME文档对象。

目前就只有一个做类似工作:

$(window.frames["iframeName"].document).bind('textselect', function(e) {

});

什么我希望做的是一样的东西:

$(window.frames["iframeName"].document,window.frames["iframeName2"].document).bind('textselect', function(e) {

});

没有正确的解决方案

其他提示

您可以匿名函数转换为命名的一个,并用它两个结合,像这样的:

$(window.frames["iframeName"].document).bind('textselect', selectStuff);
$(window.frames["iframeName2"].document).bind('textselect', selectStuff);

function selectStuff(e) {
  //Stuff
}

代替使用的匿名函数 - 创建一个名为功能

function myhandler(e)
{
    //body of function
}

然后,使用命名函数:

$(window.frames["iframeName"].document).bind('textselect', myhandler);
$(window.frames["iframeName2"].document).bind('textselect', myhandler);

另一备选

<body>
    <iframe name="iframe1" src="text.html" width="100px" height="100px" style="border:1px solid #000"></iframe>
    <iframe name="iframe2" src="text.html" width="100px" height="100px" style="border:1px solid #000"></iframe>

    <script type="text/javascript">

    $(document).ready(function() {

        function bindFrameDocuments(eventType, handler) {
            for (var i = 0; i < window.frames.length; i++) {
                $(window.frames[i].document).bind(eventType, handler);
            }
        }

        bindFrameDocuments( 
            'textselect', 
            function(e) {
                // this function runs in the scope of the frame
                window.parent.console.log(e);
            } 
        );

    });

    </script>
</body>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top