Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 查找对应的对象-使注释系统正常工作?_Javascript_Loops_For Loop_While Loop_Richtextbox - Fatal编程技术网

Javascript 查找对应的对象-使注释系统正常工作?

Javascript 查找对应的对象-使注释系统正常工作?,javascript,loops,for-loop,while-loop,richtextbox,Javascript,Loops,For Loop,While Loop,Richtextbox,当iFrameOn函数运行时,一切都应该这样做: 打开所有iFrame的设计模式 查找三个“按钮”('a'链接元素)——单击它们时会影响其相应的iFrame——这取决于它的类名 将这些“按钮”放入多维数组/对象中,即target.rtfID.x 每当单击“按钮”时,通过对象查找其对应的iFrame,并将iFrame的id作为另一个函数的参数发送 但是,当前,无论何时单击任何按钮,它们都会影响相同的iFrame。我意识到这可能是由于我使用了循环,但我不知道如何使它全部工作。控制台中没有错误 fun

当iFrameOn函数运行时,一切都应该这样做:

  • 打开所有iFrame的设计模式
  • 查找三个“按钮”('a'链接元素)——单击它们时会影响其相应的iFrame——这取决于它的类名
  • 将这些“按钮”放入多维数组/对象中,即target.rtfID.x
  • 每当单击“按钮”时,通过对象查找其对应的iFrame,并将iFrame的id作为另一个函数的参数发送 但是,当前,无论何时单击任何按钮,它们都会影响相同的iFrame。我意识到这可能是由于我使用了循环,但我不知道如何使它全部工作。控制台中没有错误

    function iFrameOn() {
        var rtfContainer, rtContainer, richTxt, richTxtId,
            rtf = document.querySelectorAll('div > form > iframe'), //Rich Text Field
            newPost = document.getElementById('richTextField').contentDocument.body,
            target = {}, rtfIndex = 0;
        //Turn iFrames On
        while (rtfIndex < rtf.length) {
            rtfID = rtf[rtfIndex].id;
            if (rtf[rtfIndex].contentDocument.designMode != 'On') {
                rtf[rtfIndex].contentDocument.designMode = 'On';
            }
            newPost.innerHTML = "<i style=\"color:#DDDDDD;\">What's up?</i>";   
            newPost.addEventListener('blur', function() {
                if (newPost.innerHTML == '') {
                    newPost.innerHTML = "<i style=\"color:#DDDDDD;\">What's up?</i>";
                }
            }, false);
            document.getElementById('richTextField').contentWindow.addEventListener(
                'focus',
                function() {
                    if (newPost.innerHTML == "<i style=\"color:#DDDDDD;\">What's up?</i>") {
                        newPost.innerHTML = '';
                    }
                },
                false
            );
            rtContainer = rtf[rtfIndex].nextElementSibling; //Next Element Sibling should be a div
            console.log('rtContainer is: '+rtContainer);
            richTxt = rtContainer.childNodes;
            console.log('richTxt is: '+richTxt);
            for (var i = 0; i < richTxt.length; i++) {
                if (richTxt[i].nodeType != 1 ||
                    (richTxt[i].nodeType == 1 &&
                        (richTxt[i].className == 'submit_button sbmtPost'
                            || richTxt[i].className == "")
                    )
                ) {
                    continue;
                }
                richTxtId = richTxt[i].id;
                target.rtfID = {};
                switch (richTxt[i].className) {
                    case 'richText bold':
                        if (target.rtfID.bold != richTxtId) {
                            target.rtfID.bold = richTxtId;
                            console.log(target.rtfID.bold+' is associated with: '+rtfID);
                        }
                        break;
                    case 'richText underline':
                        if (target.rtfID.underline != richTxtId) {
                            target.rtfID.underline = richTxtId;
                            console.log(target.rtfID.underline+' is associated with: '+rtfID);
                        }
                        break;
                    case 'richText italic':
                        if (target.rtfID.italic != richTxtId) {
                            target.rtfID.italic = richTxtId;
                            console.log(target.rtfID.italic+' is associated with: '+rtfID);
                        }
                        break;
                    default:
                        console.log('Error with commenting system!');
                        break;
                }
            }
            var obj = target.rtfID;
            for (var prop in obj) {
                if (obj.hasOwnProperty(prop)) { 
                    console.log("prop: " + prop + " value: " + obj[prop]);
                    switch(prop) {
                        case 'bold':
                            document.getElementById(obj[prop]).addEventListener(
                                'click',
                                function() {
                                    bold(obj[prop]);
                                },
                                false
                            );
                            break;
                        case 'underline':
                            document.getElementById(obj[prop]).addEventListener(
                                'click',
                                function() {
                                    Underline(obj[prop]);
                                },
                                false
                            );
                            break;
                        case 'italic':
                            document.getElementById(obj[prop]).addEventListener(
                                'click',
                                function() {
                                    Italic(obj[prop]);
                                },
                                false
                            );
                            break;
                        default: 
                            console.log('Error in for...in loop');
                    }
                } else {console.log('error');}
            }
            rtfIndex++;
        }
    }
    
    函数iFrameOn(){
    变量rtfContainer、rtContainer、richTxt、richTxtId、,
    rtf=document.querySelectorAll('div>form>iframe'),//富文本字段
    newPost=document.getElementById('richTextField').contentDocument.body,
    target={},rtfIndex=0;
    //打开iFrame
    while(rtfIndex
    您可以这样做:

    function createBolder (val) {
        return function () {
            bold(val);
        };
    }
    
    document.getElementById(obj[prop]).addEventListener(
        'click',
        createBolder(obj[prop]),
        false
    );
    
    或者,如果您真的想保持在线:

    document.getElementById(obj[prop]).addEventListener(
        'click',
        (function (val) {
            return function () {
                bold(val);
            };
        }(obj[prop])), // This function call executes immediately (i.e., while still
                       // going through the loop, so the current value of obj[prop]
                       // will be stored inside this function with its own local copy
                       // rather than using the value of obj and prop by the time
                       // the function is executed (probably the completion of the loop))
        false
    );
    

    如果没有简单的测试/检查方法,我敢打赌您的问题在于不应该在循环中定义函数——这会导致类似这样的问题。内部函数仍然可以访问外部作用域,因此在执行时,它们将获取循环中最后一个iframe的信息。您可以通过将循环中变化的值传递给一个函数来解决这个问题,该函数本身返回您想要的函数。这似乎就是问题所在。有没有办法避免在循环中定义函数?我这样做的唯一原因是因为我的富文本函数接受参数,而我不能将参数放入addEventListener方法。编辑:说话太早了。我不能在画面之间来回走动。现在它只影响找到的最后一个iFrame。