javascript - 如何使用 jquery 动态更新字体大小?

标签 javascript jquery html css

我想动态更新字体大小,所以我创建了这段代码,但问题是它不起作用。

var font_size = 36;
$('#content').on('click', function(){

  $('#editor').css('display', 'block');

  var numbersValue = "<input type=\"number\" id=\"input-fz\" value="+font_size+">";


var currentValue = $(numbersValue).prop('value');
  var eleId = $(numbersValue).prop('id');
     $(eleId).change(function(event) {
      /* Act on the event */
      newValue = $(this).val();
      newvalueFonts = newValue;
      $('h1').css('font-size', newvalueFonts);
    });

$('#result').html(numbersValue)

})

Here is my fiddle

最佳答案

可能需要三个改变

  1. 使用#作为id选择器;
  2. 将事件从 body 委托(delegate)给 input 元素。
  3. 为字体大小添加一个单位,如px

var font_size = 36;
$('#content').on('click', function() {

  $('#editor').css('display', 'block');

  var numbersValue = "<input type=\"number\" id=\"input-fz\" value=" + font_size + ">";

  var currentValue = $(numbersValue).prop('value');

  var eleId = $(numbersValue).prop('id');
   // Implementing point 1,2
  $('body').on('change', '#' + eleId, function(event) {

    /* Act on the event */
    newValue = $(this).val();
    newvalueFonts = newValue;
     // implementing point 3
    $('h1').css('font-size', newvalueFonts + 'px');
  });

  $('#result').html(numbersValue)

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1 id="content">
  Some Content Here
</h1>

<div id="editor" style="display:none">
  <div id="result"></div>
</div>

关于javascript - 如何使用 jquery 动态更新字体大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44690150/

相关文章:

javascript - 音频没有在 js 中播放

php - 避免 php 和 javascript 之间的重复代码

javascript - Express React 应用程序在本地运行,但在 heroku 部署时出现错误

javascript - 如何传后台:url dynamically using javascript to css?

php - jquery ajax问题?

html - 列不居中(CSS 格式问题)

javascript - 在 CodeIgniter 的外部 JS 文件中复制 base_url

ajax 生成的 PHP 表单无法正常工作

jquery - select2 插件和 jquery ui 模式对话框

iphone - iPhone Safari 出现奇怪结果 : OL LI A or UL LI A