[英]Function to change font-family of textarea (using option select)
I have a text field, option select, a submit button and textarea, where whatever inputted in the textfield will be display on the textarea onclick. 我有一个文本字段,选项选择,一个提交按钮和文本区域,其中在文本字段中输入的任何内容都会在onclick的文本区域上显示。 Then, depending on the selected option, the textarea style property will change also. 然后,根据所选选项,textarea样式属性也将更改。 As you can see it doesn't change according to the selected option. 如您所见,它不会根据所选选项而改变。
Heres my code. 这是我的代码。 I really am stuck here, i just started coding just 2 weeks ago, so i only have a little knowledge here. 我真的被困在这里,我只是在两周前才开始编写代码,所以我在这里只有一点知识。 Can you please tell me what was wrong here. 你能告诉我这里出了什么问题吗?
SPELLING:<input id="spell" type="text" onfocus="this.value=''" value="Input
spelling here." name="spelling">
<button onclick="document.getElementById('spell').value=''";>CLEAR </button>
<hr />
<select id="selectfonts" name="selectfonts">
<option selected disabled>Select font here...</option>
<option value="american" id="american"> American Typewriter </option>
<option value="bdmod" id="bdmod">BD Modern</option>
<option value="brush" id="brush">Brush Script</option>
</select>
<button onclick="displayspell(); selectfonts();"> SUBMIT </button>
<hr>
<textarea id="myTextarea" disabled></textarea>
<script>
function displayspell()
{
var x = document.getElementById("spell").value;
document.getElementById("myTextarea").innerHTML = x;
}
</script>
<script>
function selectfonts()
{
var s=document.getElementById('selectfonts');
var selectfonts = s.option[s.selectedIndex].value;
if(selectfonts == 'american')
{
document.getElementById('myTextarea').style.font-family ='american';
}
else if(selectfonts == 'bdmod')
{
document.getElementById('myTextarea').style.font-family = 'bdmod';
}
else if(selectfonts == 'brush')
{
document.getElementById('myTextarea').style.font-family = 'brush';
}
else
{
alert('Input spelling or choose font!');
}
}
</script>
css code: CSS代码:
@font-face
{ font-family:'american';
src:url('American Typewriter.ttf') format('truetype');}
@font-face
{ font-family:'bdmod';
src:url('BASKVILL.TTF') format('truetype');}
@font-face
{ font-family:'brush';
src:url('BRUSHSCI.TTF') format('truetype');}
#american{font-family:'american';}
#bdmod{font-family: 'bdmod';}
#brush{font-family: 'brush';}
The proper syntax is: style.fontFamily
. 正确的语法是: style.fontFamily
。
Example : 范例 :
document.getElementById('myTextarea').style.fontFamily = 'brush';
Simplifying your code: 简化代码:
Since you're applying the font name retrieved from the selected value, applying it directly. 由于您要应用从所选值中检索到的字体名称,因此请直接应用它。
function selectfonts()
{
var s=document.getElementById('selectfonts');
var selectfonts = s.option[s.selectedIndex].value;
if(selectfonts != "")
document.getElementById('myTextarea').style.fontFamily = selectfonts;
else alert("Please choose a font...");
}
As already pointed out, the main issue is that style.fontFamily
is the proper syntax. 正如已经指出的,主要问题是style.fontFamily
是正确的语法。
Some additional things to look into to make your code better: 为了使您的代码更好,还需要考虑一些其他事项:
s
is not a very good choice. s
不是一个很好的选择。 Take a look at this javascript which is much shorter and more readable. 看一下这个JavaScript,它更短,更易读。
const clearButton = document.getElementsByClassName('clear-button')[0], spellingInput = document.getElementsByClassName('spelling-input')[0], fontsSelect = document.getElementsByClassName('fonts-select')[0], styledTextArea = document.getElementsByClassName('styled-text-area')[0]; clearButton.addEventListener('click', function() { spellingInput.value = ''; styledTextArea.value = ''; }); spellingInput.addEventListener('input', function() { styledTextArea.value = spellingInput.value; }); fontsSelect.addEventListener('change', function() { styledTextArea.style.fontFamily = fontsSelect.value; });
<label>SPELLING:</label><input class="spelling-input" type="text" placeholder="Input spelling here."> <button class="clear-button">CLEAR</button> <hr /> <select class="fonts-select"> <option selected disabled>Select font here...</option> <option value="sans-serif">sans-serif</option> <option value="serif">serif</option> <option value="monospace">monospace</option> </select> <hr> <textarea class="styled-text-area" disabled></textarea>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.