简体   繁体   English

JavaScript无效参数?

[英]Javascript Invalid Argument?

I'm new to JavaScript so please bear with me. 我是JavaScript新手,请耐心等待。 I'm working on a custom WYSIWYG editor (no I do not want to use an already made one so please don't suggest it). 我正在使用自定义WYSIWYG编辑器(不,我不想使用已经制作好的编辑器,所以请不要提出建议)。

I'm having trouble getting it to work in Internet Explorer. 我无法使其在Internet Explorer中正常工作。 It gives me an Invalid Arguement error on lines such as the following: 它使我在诸如以下这样的行上出现了无效参数错误:

document.getElementById('editor').contentWindow.document.execCommand("styleWithCSS", false, "false");

Here is the complete script: 这是完整的脚本:

function textstyle(a) {
    document.getElementById(a).style.visibility = 'visible';
    document.getElementById('editor').contentWindow.focus();
}

function option(a,b) {
    document.getElementById('editor').contentWindow.document.execCommand(a, false, b);
    document.getElementById('editor').contentWindow.focus();
}

function button(a) {
    document.getElementById('editor').contentWindow.document.execCommand(a, false, null);
    document.getElementById('editor').contentWindow.focus();
}

var colorSelection;

function selectColor(selection) {
    colorSelection = selection;
    document.getElementById('colorSelector').style.left = 0 + document.getElementById(selection).offsetLeft + "px";
    document.getElementById('colorSelector').style.top = 0 + document.getElementById(selection).offsetTop + document.getElementById(selection).offsetHeight + "px";
    document.getElementById('colorSelector').style.visibility = 'visible';
    return;
}

function changeColor(colorCode) {
    document.getElementById('editor').contentWindow.document.execCommand(colorSelection, false, colorCode);
    document.getElementById('colorSelector').style.visibility = 'hidden';
    document.getElementById('editor').contentWindow.focus();
    return;
}

function dismissmenu()
{
    document.getElementById("colorSelector").style.visibility = 'hidden';
    document.getElementById("fontlist").style.visibility = 'hidden';
    document.getElementById("formatlist").style.visibility = 'hidden';
    document.getElementById("sizelist").style.visibility = 'hidden';
}

function Start() {
    document.getElementById('editor').contentWindow.document.designMode = "on";
    document.getElementById('editor').contentWindow.document.execCommand("styleWithCSS", false, "false");

    try {
        document.getElementById('editor').contentWindow.document.execCommand("undo", false, null);
        editormode = "true";
    }  catch (e) {
        editormode = "false";
    }

    if (document.addEventListener) {
        document.addEventListener("mouseup", dismissmenu, true);
        document.getElementById("editor").contentWindow.document.addEventListener("mouseup", dismissmenu, true);
        document.addEventListener("keypress", dismissmenu, true);
        document.getElementById("editor").contentWindow.document.addEventListener("keypress", dismissmenu, true);
    } else if (document.attachEvent) {
        document.attachEvent("mouseup", dismissmenu, true);
        document.getElementById("editor").contentWindow.document.attachEvent("mouseup", dismissmenu, true);
        document.attachEvent("keypress", dismissmenu, true);
        document.getElementById("editor").contentWindow.document.attachEvent("keypress", dismissmenu, true);
    }
}

function switchEditorMode() {
    if (editormode == "true") {

        var replaceTagsByMode = function(html, editormode) {
            var tags = {};
            for (var i=0, a=['b', 'i', 'u', 'strike', 'sub', 'sup']; i<a.length; i++) {
                tags[['<', a[i], '>'].join('')] = ['[', a[i], ']'].join('');
                tags[['</', a[i], '>'].join('')] = ['[/', a[i], ']'].join('');
            }
            for (var html_tag in tags) {
                if (tags.hasOwnProperty(html_tag)) {
                    html = html.replace.apply(
                    html, editormode ? [html_tag, tags[html_tag], 'g'] : [tags[html_tag], html_tag, 'g']);
                }
            }
            return html;
        };

        var editor_body = document.getElementById('editor').contentWindow.document.body;
        editor_body.innerHTML = replaceTagsByMode(editor_body.innerHTML, editormode);


        editormode = "false";

    } else {

        var replaceTagsByMode = function(html, editormode) {
            var tags = {};
            for (var i=0, a=['b', 'i', 'u', 'strike', 'sub', 'sup']; i<a.length; i++) {
                tags[['[', a[i], ']'].join('')] = ['<', a[i], '>'].join('');
                tags[['[/', a[i], ']'].join('')] = ['</', a[i], '>'].join('');
            }
            for (var html_tag in tags) {
                if (tags.hasOwnProperty(html_tag)) {
                    html = html.replace.apply(
                    html, editormode ? [html_tag, tags[html_tag], 'g'] : [tags[html_tag], html_tag, 'g']);
                }
            }
            return html;
        };

        var editor_body = document.getElementById('editor').contentWindow.document.body;
        editor_body.innerHTML = replaceTagsByMode(editor_body.innerHTML, editormode);


        editormode = "true";

    }
}

从MSDN上的文档,看来StyleWithCSS不是命令识别符存在。

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

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