我在簡單網站內有一個文本區域,用戶可以在其中鍵入他們喜歡的內容。我想添加一個選擇器(下拉框/ Combobox)來改變所述文本區域內所有文本的顏色。使用<select>和Javascript更改文本區域中文本的顏色
全碼
function Text() {
if(document.getElementById("textarea").style.fontWeight != 'bold')
document.getElementById("textarea").style.fontWeight = 'bold';
else
document.getElementById("textarea").style.fontWeight = 'normal';
}
function Text() {
if(document.getElementById("textarea").style.fontStyle != 'italic')
document.getElementById("textarea").style.fontStyle = 'italic';
else
document.getElementById("textarea").style.fontStyle = 'normal';
}
function Text() {
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;
}
body {
padding: 0px;
margin: auto;
display: block;
width: 10px;
height: 7px;
position: center;
}
h1 {
font-family: Arial, Helvetica, sans-serif;
font-size: 112px;
color: #C0C0C0;
text-align: center;
}
textarea {
width: 90%;
height: 450px;
padding: 12px 20px;
box-sizing: border-box;
border: 2px solid;
background-color:;
font-size: 16px;
resize: none;
}
#Button {
position: relative;
top: 450px;
left: 50px;
}
#Button {
position: relative;
top: 450px;
left: 70px;
}
#Button {
position: relative;
top: 450px;
left: 90px;
}
select {
position: relative;
top: -302px;
left: 320px;
}
<!doctype html>
<html>
<head>
\t <title>Simple Word Processor</title>
\t
\t
</head>
<body>
<button id="Button" type="button" onclick="boldText()">Bold</button>
<button id="Button" type="button" onclick="italicText()">Italic</button>
<button id="Button" type="button" onclick="underlineText()">Underline</button>
<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>
\t
</body>
</html>
'document'中的'id'應該是唯一的。最多隻能有一個'#textarea'元素。如果你正在嘗試選擇一個'textarea'元素,你可以使用'var list = document.querySelector(「textarea」)''''list'元素來設置'.color'而不用'for'循環。 – guest271314