gpt4 book ai didi

javascript - 如何用数字划分 div 并将结果值作为字体大小

转载 作者:太空宇宙 更新时间:2023-11-04 09:47:38 25 4
gpt4 key购买 nike

我动态制作了 div 和 H2,所以现在我希望 h2 的大小等于 DIV-width/20,这意味着如果 div 宽度为 400px,则字体大小应为 20px。所以这就是我所做的:

var div2 = document.createElement('div');  
div2.className = "div2" ;
div2.id = "div2";
var h2 = document.createElement('h2');
h2.innerHTML= "GET AN ESTIMATE OVER VIDEO CHAT";
h2.className = "naslov";

var DivWidtx = document.getElementById("div2").getBoundingClientRect().width;
alert(DivWidtx); // - this is working good
var fontSize = DivWidtx / 20;
alert(fontSize);

所以这个脚本不起作用,有人可以帮我怎么做吗?
所以我想要 divWidth/20 = fontSize, and H2 { font-size = fontSize; } h2.style.fontSize = fontSize;

最佳答案

试试这个:

var fontSize = (parseInt(DivWidtx) / 20 ) + 'px';

取而代之的是:

var fontSize = (DivWidtx / 20) ;

关于javascript - 如何用数字划分 div 并将结果值作为字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39365261/

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