使用<select>和Javascript更改文本区域中文本的颜色

时间:2017-02-20 05:29:42

标签: javascript

我在一个简单的网站中有一个文本区域,用户可以输入他们喜欢的内容。我想添加一个选择器(Dropdown box / Combobox)来改变所述文本区域内所有文本的颜色。 完整代码 &#13; &#13; function Text(){&#13;         if(document.getElementById(&#34; textarea&#34;)。style.fontWeight!=&#39; bold&#39;)&#13;             document.getElementById(&#34; textarea&#34;)。style.fontWeight =&#39; bold&#39 ;;&#13;         否则&#13;             document.getElementById(&#34; textarea&#34;)。style.fontWeight =&#39; normal&#39 ;;&#13;     }&#13;     function Text(){&#13;         if(document.getElementById(&#34; textarea&#34;)。style.fontStyle!=&#39; italic&#39;)&#13;             document.getElementById(&#34; textarea&#34;)。style.fontStyle =&#39; italic&#39;;&#13;         否则&#13;             document.getElementById(&#34; textarea&#34;)。style.fontStyle =&#39; normal&#39 ;;&#13;     }&#13;     function Text(){&#13;         if(document.getElementById(&#34; textarea&#34;)。style.textDecoration!=&#39; underline&#39;)&#13;             document.getElementById(&#34; textarea&#34;)。style.textDecoration =&#39; underline&#39;;&#13;         否则&#13;             document.getElementById(&#34; textarea&#34;)。style.textDecoration =&#39; none&#39 ;;&#13;     }&#13;      &#13;      &#13;     document.getElementById(&#39; colorChanger&#39;)。addEventListener(&#39; change&#39;,changeColor);&#13;      &#13;     function changeColor(){&#13;     var color = document.getElementById(&#39; colorChanger&#39;)。value;&#13;     var list = document.getElementById(&#39; textarea&#39;);&#13;     list.style.color =颜色;&#13;      &#13;      &#13;     }&#13;     身体{&#13;         填充:0px;&#13;         保证金:自动;&#13;         display:block;&#13;         宽度:10px;&#13;         身高:7px;&#13;         位置:中心;&#13; &#13;     }&#13;      &#13;     h1 {&#13;         font-family:Arial,Helvetica,sans-serif;&#13;         font-size:112px;&#13;         颜色:#C0C0C0;&#13;         text-align:center;&#13;      &#13;     }&#13;      &#13;     textarea {&#13;         宽度:90%;&#13;         身高:450px;&#13;         填充:12px 20px;&#13;         box-sizing:border-box;&#13;         边框:2px实体;&#13;         背景色:;&#13;         font-size:16px;&#13;         调整大小:无;&#13;     }&#13;      &#13;     #Button {&#13;         位置:相对;&#13;         上:450px;&#13;         左:50px;&#13;     }&#13;      &#13;     #Button {&#13;         位置:相对;&#13;         上:450px;&#13;         左:70px;&#13;     }&#13;      &#13;     #Button {&#13;         位置:相对;&#13;         上:450px;&#13;         左:90px;&#13;     }&#13;     选择{&#13;         位置:相对;&#13;         上:-302px;&#13;         左:320px;&#13;     }&#13; &lt;!doctype html&gt;&#13;     &#13;     &LT; HTML&GT;&#13;     &LT; HEAD&GT;&#13; &#13;     &lt; title&gt;简单字处理器&lt; / title&gt;&#13;     &#13;     &#13;     &#13;   &#13;     &#13;     &#13; &#13;     &#13;     &#13;     &#13;     &LT; /头&GT;&#13;     &#13;     &#13;     &#13;     &LT;身体GT;&#13;     &#13;         &lt; button id =&#34; Button&#34;类型=&#34;按钮&#34;的onclick =&#34; boldText()&#34;&GT;粗体&LT; /按钮&GT;&#13;         &lt; button id =&#34; Button&#34;类型=&#34;按钮&#34;的onclick =&#34; italicText()&#34;&GT;斜体&LT; /按钮&GT;&#13;         &lt; button id =&#34; Button&#34;类型=&#34;按钮&#34;的onclick =&#34; underlineText()&#34;&GT;下划线&LT; /按钮&GT;&#13;        &#13; &#13; &#13;      &#13;         &lt; form id =&#34; form&#34;&gt;&#13;             &lt; textarea id =&#34; textarea&#34;&gt;在这里输入文字...&lt; / textarea&gt;&#13;         &LT; /形式&GT;&#13;        &#13;     &#13;      &#13;     &lt; select id =&#34; colorChanger&#34;&gt;&#13;        &lt; option value =&#34;#000&#34;&gt; Black&lt; / option&gt;&#13;        &lt; option value =&#34;#f00&#34;&gt; Red&lt; / option&gt;&#13;        &lt; option value =&#34;#00f&#34;&gt; Blue&lt; / option&gt;&#13;        &lt; option value =&#34;#0f0&#34;&gt; Green&lt; / option&gt;&#13;     &LT; /选择&GT;&#13;        &#13;     &#13;     &#13;     &LT; /体&GT;&#13;     &#13;     &#13;     &#13;     &#13;     &#13;     &#13;     &LT; / HTML&GT;&#13; &#13; &#13;

4 个答案:

答案 0 :(得分:2)

getElementById不会返回数组

&#13;
&#13;
document.getElementById('colorChanger').addEventListener('change', changeColor);

function changeColor() {
var color = document.getElementById('colorChanger').value;
var list = document.getElementById('textarea1');
list.style.color=color;


}
&#13;
<textarea id="textarea1">Enter text here...</textarea>


<select id="colorChanger">
   <option value="#000">black</option>
   <option value="#f00">Red</option>
   <option value="#00f">Blue</option>
   <option value="#0f0">Green</option>
</select>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

只需设置style.color,如下所示。

var list = document.getElementById('textarea');
list.style.color = color;

&#13;
&#13;
document.getElementById('colorChanger').addEventListener('change', changeColor);

function changeColor() {
var color = document.getElementById('colorChanger').value;
var list = document.getElementById('textarea');
list.style.color = color;
}
&#13;
<textarea id="textarea">Enter text here...</textarea>


<select id="colorChanger">
   <option value="black">black</option>
   <option value="red">Red</option>
   <option value="yellow">Yellow</option>
   <option value="green">Green</option>
</select>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

为你here创建了一个文件。

使用传统的javascript:

<script type="text/javascript">
function abc(val){
document.getElementById("textarea").style.color=val;
}
</script>

<textarea id="textarea">Enter text here...</textarea>


<select id="colorChanger"  onmousedown="this.value='';" onchange="abc(this.value)">
   <option value="black">black</option>
   <option value="red">Red</option>
   <option value="yellow">Yellow</option>
   <option value="green">Green</option>
</select>

Updatee:

在yopu代码中添加:

<script>
 function changeColor() {
    var color = document.getElementById('colorChanger').value;
   document.getElementById("textarea").style.color=color;
}</script>

在样式标记之后的标题中。

你也没有调用select on tag的函数。在select标签中添加。

<select id="colorChanger" onchange="changeColor()">

答案 3 :(得分:0)

您将我之前的代码与您的问题中的评论相结合,我将getElementById更改为getElementByTagNamegetElementsByTagName您错过了s,它将返回数组,而getElementById将返回一个对象

&#13;
&#13;
 
function boldText() {
    if(document.getElementById("textarea").style.fontWeight != 'bold')
        document.getElementById("textarea").style.fontWeight = 'bold';
    else
        document.getElementById("textarea").style.fontWeight = 'normal';
}
function italicText() {
    if(document.getElementById("textarea").style.fontStyle != 'italic')
        document.getElementById("textarea").style.fontStyle = 'italic';
    else
        document.getElementById("textarea").style.fontStyle = 'normal';
}
function underlineText() {
    if(document.getElementById("textarea").style.textDecoration != 'underline')
        document.getElementById("textarea").style.textDecoration = 'underline';
    else
        document.getElementById("textarea").style.textDecoration = 'none';
}
 
 
document.getElementById('colorChanger').addEventListener('change', changeColor);
 
function changeColor() {
var color = document.getElementById('colorChanger').value;
var list = document.getElementById('textarea');
list.style.color=color;
 
 
}
 
&#13;
 
body {
    border: 3px solid #C0C0C0 ;
    padding: 0px;
    margin: auto;
    display: block;
    width: 1000px;
    height: 700px;
    position: center;
    outline-style: solid;
    outline-color: #f8f8f8;
    outline-width: 10000px;
}
 
h1 {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 112px;
    color: #C0C0C0;
    position: relative;
    top: -220px;
    text-align: center;
 
}
 
textarea {
    width: 90%;
    height: 450px;
    padding: 12px 20px;
    box-sizing: border-box;
    border: 2px solid #C0C0C0;
    border-radius: 4px;
    background-color: #f8f8f8;
    font-size: 16px;
    resize: none;
    position: relative;
    top: -305px;
    left: 50px;
}
 
#boldButton {
    position: relative;
    top: 450px;
    left: 50px;
}
 
#italicButton {
    position: relative;
    top: 450px;
    left: 70px;
}
 
#underlineButton {
    position: relative;
    top: 450px;
    left: 90px;
}
select {
    position: relative;
    top: -302px;
    left: 320px;
}  
   
 
&#13;
<!doctype html>
 
<html>
<head>
  <meta charset="UTF-8">
    <title>Simple Word Processor</title>
</head>
<body>
 
    <button id="boldButton" type="button" onclick="boldText()">Bold</button>
    <button id="italicButton" type="button" onclick="italicText()">Italic</button>
    <button id="underlineButton" type="button" onclick="underlineText()">Underline</button>
   
    <canvas></canvas>
 
    <h1> Word Processor </h1>
 
    <form id="form">
        <textarea id="textarea">Enter text here...</textarea>
    </form>
   
 
 
 
 
 
<select id="colorChanger">
   <option value="#000">Black</option>
   <option value="#f00">Red</option>
   <option value="#00f">Blue</option>
   <option value="#0f0">Green</option>
</select>
   
</body> 
</html>
&#13;
&#13;
&#13;