2010-08-19 3 views
0

Я новичок в JavaScript, поэтому, пожалуйста, медведь со мной. Я работаю над пользовательским редактором WYSIWYG (нет, я не хочу использовать уже сделанный, поэтому, пожалуйста, не предлагайте его).Неверный аргумент Javascript?

У меня возникли проблемы с его работой в Internet Explorer. Это дает мне ошибку Invalid Arguement на линиях, таких как:

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

Вот полный скрипт:

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"; 

    } 
} 
+0

'пожалуйста, со мной. Извините, но я не думаю, что мы уже готовы к этому шагу. – Pointy

ответ

2

Из документации по MSDN, представляется, что StyleWithCSS не command identifier, что существует ,

+0

Я вижу. Я просмотрел некоторую информацию об этом руководстве. https://developer.mozilla.org/ru/Rich-Text_Editing_in_Mozilla Возможно, мне придется определить, является ли браузер FF чем стиль с HTML – Cory

+0

@Cory: не пытайтесь обнаружить FF. Вместо этого используйте «обнаружение функции» http://www.jibbering.com/faq/notes/detect-browser/ – dolmen

+0

Oded правильно, «StyleWithCSS» был введен Mozilla и не был реализован в IE. Вы можете легко обнаружить его, окружая вызов команды с помощью 'try ... catch'. –

Смежные вопросы