gpt4 book ai didi

javascript - 如何通过 javascript 加减 css 字体大小?

转载 作者:行者123 更新时间:2023-11-30 10:15:49 28 4
gpt4 key购买 nike

我将用户提供的 html 保存在数据库中,“指定或不指定”css 字体大小。

并尝试制作一个按钮来放大或减小 dom 上的字体大小,但保持相对大小。

我将变量 def_size 定义为 16px,并将 cur_size 定义为动态的基本大小。

但我不知道如何处理预定义大小的dom?我尝试了preg_replace,但失败了。

http://jsfiddle.net/95kDZ/

function change_font_size(csize){
var $cotent_html=$('.content');
cur_base=cur_base+csize;
//without style,just add the css to the whole div"
$cotent_html.css('font-size',cur_base.toString()+'px');
/*

请给我一些提示。谢谢

最佳答案

我认为您需要获取每个可能包含文本的元素:pspandiv 等,然后应用您的 font-size 逻辑而不是整个 html 根。您需要针对特定​​的 html 元素以覆盖其样式。

更新了工作 fiddle here .

代码:

var def_size=16,
cur_base=def_size;

$('input[type="button"]').click(function() {
change_font_size(parseInt($(this).data('csize')));
});

function change_font_size(csize){
var $cotent_html=$('.content');

cur_base=cur_base+csize;
//without style,just add the css to the whole div"
$cotent_html.find('span, p, div').css('font-size', cur_base.toString()+'px');
}

关于javascript - 如何通过 javascript 加减 css 字体大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23733582/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com