Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Textarea、按钮和javascript设置自定义字体大小_Javascript_Html_Css - Fatal编程技术网

使用Textarea、按钮和javascript设置自定义字体大小

使用Textarea、按钮和javascript设置自定义字体大小,javascript,html,css,Javascript,Html,Css,我正在尝试设置一些代码,用户可以在中输入一个数字,然后单击一个按钮,该按钮将通过编辑Style属性来设置另一个的字体大小。我尝试了以下方法: <h2>Set Font</h2> <textarea id='input'></textarea> <button onclick ="myFunction()">Click</button> <textarea id='MainText' style=&q

我正在尝试设置一些代码,用户可以在
中输入一个数字,然后单击一个按钮,该按钮将通过编辑
Style
属性来设置另一个
的字体大小。我尝试了以下方法:

<h2>Set Font</h2>
<textarea id='input'></textarea>
<button onclick ="myFunction()">Click</button>
<textarea id='MainText' style="font-size:18px;"></textarea>
<script>
function myFunction(){
  var InputValue = document.getElementById('input').value;
  document.getElementById('MainText').style.font-size = 'InputValue';
  alert('Font Size Changed to ${InputValue}')
}
设置字体
点击
函数myFunction(){
var InputValue=document.getElementById('input').value;
document.getElementById('MainText')。style.font-size='InputValue';
警报('字体大小更改为${InputValue}')
}

我不知道该怎么办。

这里有一些语法错误。您使用的是JS,因此需要使用camelCase
fontSize
。另外,当您应该将其分配给变量名时,您正在将其分配给字符串,因此请去掉这些引号。此外,您在警报中错误地插入了变量:
alert('Font Size更改为${InputValue}')
对此需要使用反勾号,而不是单引号

var InputValue = document.getElementById('input').value;
document.getElementById('MainText').style.font-size = 'InputValue';
应该是:

var inputValue = document.getElementById('input').value;
document.getElementById('MainText').style.fontSize = inputValue + 'px';

这里有一些语法错误。您使用的是JS,因此需要使用camelCase
fontSize
。另外,当您应该将其分配给变量名时,您正在将其分配给字符串,因此请去掉这些引号。此外,您在警报中错误地插入了变量:
alert('Font Size更改为${InputValue}')
对此需要使用反勾号,而不是单引号

var InputValue = document.getElementById('input').value;
document.getElementById('MainText').style.font-size = 'InputValue';
应该是:

var inputValue = document.getElementById('input').value;
document.getElementById('MainText').style.fontSize = inputValue + 'px';

您应该使用fontSize而不是font size,并将inputValue作为变量而不是字符串 e、 g


您应该使用fontSize而不是font size,并将inputValue作为变量而不是字符串 e、 g


我不知道该如何添加警报。@Coolsugar有引号(“)、单引号(”)和反引号(`)…这三种不同的东西。引号和单引号可以互换使用,但反引号对于字符串插值(或称为模板文字)有一个特殊的用例。在mac电脑上,它是键盘上数字1左边的按钮。您可以在这里看到更多信息:我不知道如何添加警报。@Coolsugar有引号(“)、单引号(”)和反勾号(`)…所有3种不同的东西。引号和单引号可以互换使用,但反勾号对于字符串插值(或称为模板文字)有一个特殊的用例。在mac电脑上,它是键盘上数字1左边的按钮。您可以在此处看到更多信息: