简体   繁体   English

这在IE中有效,但在chrome中不起作用。 如何使它在所有浏览器中都能正常工作?

[英]This works in IE but not chrome. How do I get it to work in all browsers?

This works great in IE, but I tested it in chrome and it does not work. 这在IE中效果很好,但我在chrome中对其进行了测试,但无法正常工作。 First I create a text area with HTML tags and color drop down selection. 首先,我创建一个带有HTML标记和颜色下拉选择的文本区域。 It should write into the text area "This is ap tag" and in the color that you pick from the drop down selection. 它应以您从下拉选择中选择的颜色写入“ This is ap tag”文本区域。 jsFiddle . jsFiddle I only know javascript, haven't started to learn jQuery yet. 我只知道javascript,还没有开始学习jQuery。

function createTextArea() {
    var textAreaElement = document.createElement("textarea");
    textAreaElement.id = "textarea";
    textAreaElement.rows = "13";
    textAreaElement.cols = "25";
    document.body.appendChild(textAreaElement);
}

function textArea() {
    var textarea = document.getElementById("textarea");
    var mylist = document.getElementById("myList");
    var options = mylist.options[mylist.selectedIndex].text;
    var color = textcolor.options[textcolor.selectedIndex].text;
    var element = document.createElement(options);
    var mytext;
    var textColor;

    switch (options) {
        case "p":
            mytext = "I am a paragraph tag.";
            break;
        case "div":
            mytext = "I am a div tag.";
            break;
        case "span":
            mytext = "I am a span tag.";
            break;
        case "strong":
            mytext = "I am a strong tag.";
            break;
        case "h1":
            mytext = "I am a h1 tag.";
            break;
        case "em":
            mytext = "I am a em tag.";
            break;
    }

    switch (color) {
        case "Red":
            textColor = "red";
            break;
        case "Blue":
            textColor = "blue";
            break;
        case "Pink":
            textColor = "pink";
            break;
        case "Green":
            textColor = "green";
            break;
        case "Crimson":
            textColor = "crimson";
            break;
        case "Yellow":
            textColor = "yellow";
            break;
        case "Dark Slate Blue":
            textColor = "DarkSlateBlue";
            break;
        case "Dark Orange":
            textColor = "DarkOrange";
            break;
        case "Dark Violet":
            textColor = "DarkViolet";
            break;
        case "Deep Pink":
            textColor = "DeepPink";
            break;
        case "Fuchsia":
            textColor = "Fuchsia";
            break;
        case "Lime":
            textColor = "Lime";
            break;
        case "Maroon":
            textColor = "Maroon";
            break;
        case "Saddle Brown":
            textColor = "SaddleBrown";
            break;
        case "Teal":
            textColor = "Teal";
            break;
    }
    var text = document.createTextNode(mytext);
    element.style.color = textColor;
    element.appendChild(text);
    textarea.appendChild(element);
}

function textAreaElements() {
    var selectElement = document.createElement("select");
    selectElement.id = "myList";
    selectElement.onchange = textArea;

    for (i = 0; i < 7; i++) {
        var options = document.createElement("option");
        var elements;
        switch (i) {
            case 0:
                elements = " ";
                break;
            case 1:
                elements = "p";
                break;
            case 2:
                elements = "div";
                break;
            case 3:
                elements = "span";
                break;
            case 4:
                elements = "strong";
                break;
            case 5:
                elements = "h1";
                break;
            case 6:
                elements = "em";
                break;
        }
        var text = document.createTextNode(elements);
        document.body.appendChild(selectElement);
        selectElement.appendChild(options);
        options.appendChild(text);
    }
}

function textAreaColor() {
    var selectElement = document.createElement("select");
    selectElement.id = "textcolor";
    selectElement.onchange = textArea;
    for (i = 0; i < 16; i++) {
        var options = document.createElement("option");
        var color;
        switch (i) {
            case 0:
                color = " ";
                break;
            case 1:
                color = "Red";
                break;
            case 2:
                color = "Blue";
                break;
            case 3:
                color = "Pink";
                break;
            case 4:
                color = "Green";
                break;
            case 5:
                color = "Crimson";
                break;
            case 6:
                color = "Yellow";
                break;
            case 7:
                color = "Dark Slate Blue";
                break;
            case 8:
                color = "Dark Orange";
                break;
            case 9:
                color = "Dark Violet";
                break;
            case 10:
                color = "Deep Pink";
                break;
            case 11:
                color = "Fuchsia";
                break;
            case 12:
                color = "Lime";
                break;
            case 13:
                color = "Maroon";
                break;
            case 14:
                color = 'Saddle Brown';
                break;
            case 15:
                color = 'Teal';
                break;
        }

        var text = document.createTextNode(color);
        document.body.appendChild(selectElement);
        selectElement.appendChild(options);
        options.appendChild(text);
    }
}      

createTextArea();
textAreaElements();
textAreaColor();

I think this change is what you want, I don't believe children are allowed within a textarea 我认为此更改是您想要的,我不认为在文textarea允许孩子

Replaceable character data In documents in the HTML syntax, the title and textarea elements can contain replaceable character data. 可替换字符数据在HTML语法的文档中,title和textarea元素可以包含可替换字符数据。 Replaceable character data can contain the following: 可替换字符数据可以包含以下内容:

text, optionally including "<" characters character references Replaceable character data has the following restrictions: 文本,可以选择包括“ <”个字符字符引用可替换字符数据具有以下限制:

must not contain any ambiguous ampersands must not contain any occurrences of the string "", or "/". 不得包含任何歧义的“&”号,也不得包含任何出现的字符串“”或“ /”。

textarea.style.color = textColor;
//element.appendChild(text);
//textarea.appendChild(element);
textarea.value = mytext;

暂无
暂无

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

相关问题 Polymer:点击事件在IE中有效,但在Chrome中不起作用。 怎么修? - Polymer: on-click event works in IE but not Chrome. How to fix? 复选框输入在chrome中根本不起作用。 在其他浏览器中不起作用 - Checkbox Input doesn't work at all in chrome. Doesn't function in other browsers 如何获得适用于所有浏览器(IE10+)的这种形式的 div? - How to get a div of such form that works in all browsers (IE10+)? HTML5游戏 - 适用于IE,Chrome。 在FireFox中失败...直到我导航到.mp3文件...然后它工作 - HTML5 Game - Works in IE, Chrome. Fails in FireFox…Until I Navigate to a .mp3 File…Then It Works 链接在 <select>使用JS不适用于Chrome。 IE&FF它的工作原理 - Link in a <select> with JS doesn't work on Chrome. IE & FF it works 工作剪贴板脚本与Chrome不兼容。 适用于Edge / IE - Working clipboard script not compatible with Chrome. Works with Edge/IE jQuery选项卡不适用于Firefox和chrome。 在IE 9上也可以使用 - Jquery Tab not working on Firefox and chrome. Same works on IE 9 有人知道 IE 中这段代码的替代品是什么吗?..它适用于 firefox,chrome.( = () =&gt; ) - Anybody knows what is the replacement for this code in IE?..It works in firefox,chrome.( = () => ) 如何定位除IE和Edge以外的所有浏览器 - How do I target all browsers except for IE and Edge 我写了一个选项卡控件,但无法在所有浏览器中都使用CSS,该如何解决? - I wrote a tab control but I cannot get the css to work in all browsers, how do I fix it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM