简体   繁体   English

更改文本区域的字体系列的功能(使用选项选择)

[英]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: 为了使您的代码更好,还需要考虑一些其他事项:

  • Choose more informative variable names. 选择更多有用的变量名。 s is not a very good choice. s不是一个很好的选择。
  • Use event listeners instead of inline javascript method calls. 使用事件侦听器,而不要使用内联javascript方法调用。
  • Use anonymous inner functions for simple specific tasks. 将匿名内部函数用于简单的特定任务。
  • Offload work from the user - there is no need to make the user press a submit button for a simple task like this. 减轻用户的工作负担-无需让用户按下“提交”按钮即可完成这样的简单任务。
  • Use const or let instead of var - using const tells other programmers that this value will not be changing 使用const或let代替var-使用const告诉其他程序员,该值不会改变
  • Use classes instead of ids. 使用类而不是ID。
  • Declare all variables, validate data (not done here), then perform actions. 声明所有变量,验证数据(此处未完成),然后执行操作。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM