简体   繁体   English

如何使用输入类型编号更改字体大小?

[英]How to change the font size with an input type number?

该图显示了理想的结果

The image above shows a function for a user to select his or her preferenced font size (it's a website for all ages). 上图显示了一个功能,用户可以选择自己喜欢的字体大小(这是所有年龄段的网站)。 However, transferring this to code is easier said than done for me. 但是,将其转移到代码上对我来说说起来容易做起来难。 All I found was that I had to use the following method: 我发现的是我必须使用以下方法:

<input type="number">

Is there anyone who knows how to make such a function work with Javascript or jQuery that adjusts the font size to the selected number? 有谁知道如何使这种功能与将字体大小调整为所选数字的Javascript或jQuery一起使用?

Thanks so much in advance! 非常感谢!

You can pull the val() of the input on a submission and set the font size using .css() : 您可以在提交中提取输入的val()并使用.css()设置字体大小:

JS JS

$("input[type=submit]").click(function(){
    var fontSize = $("input[type=number]").val();
    $("p").css({"font-size":fontSize+"px"});
});

HTML 的HTML

<input type="number" placeholder="choose font-size"/>
<input type="submit"/>
<p>Hello</p>

BUTTON EXAMPLE 按钮示例

OR 要么

You could do it on a keyup or keydown event (start typing your number): 您可以在keyupkeydown事件(开始输入您的电话号码)上进行操作:

$("input[type=number]").keyup(function(){
    var fontSize = $(this).val();
    $("p").css({"font-size":fontSize+"px"});    
});

KEYUP EXAMPLE KEYUP示例

OR 要么

To change the value when clicking the up.down arrows, you can use .change() 要在单击向上箭头或向下箭头时更改值,可以使用.change()

$("input[type=number]").change(function(){
   var fontSize = $(this).val();
   $("p").css({"font-size":fontSize+"px"}); 
});

CHANGE EXAMPLE 更改示例

This is the answer for your question 这是您的问题的答案

CSS 的CSS

input[type=number] {
font-size: 31px;
}

You can do something like this: 您可以执行以下操作:

 <input type="text" class="inp">

 .inp { font-size:20px; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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