猿问
如何在 JavaScript 中创建一个将文本增加 1px 的按钮
我需要你的帮助。我是 JavaScript 初学者,我想创建一个将文本增加 1px 的按钮。你能帮助我吗?
撒科打诨
浏览 144
回答 3
3回答
交互式爱情
这应该有帮助!window.getComputedStyle(txt, null).getPropertyValue('font-size');您可以使用它来获取fontSize元素的 。const txt = document.querySelector("#txt");function changeFontSize(){ let fontSize = window.getComputedStyle(txt, null).getPropertyValue('font-size'); let newFontSize = Number(fontSize.slice(0,2)) + 1 txt.style.fontSize = `${newFontSize}px`; }#txt { font-size: 16px; }<div id="txt">Sample Text</div><button onclick="changeFontSize()">Click</button>
0
0
0
守着一只汪
有很多方法可以做到这一点,如上面的 Som 重放我的方式<body> <p class="text" style="color:red">Hello Ahmed</p> <button class="btn">Click Me</button> <script> x = 1; document.querySelector(".btn").addEventListener("click",function(){ increseText(); }) function increseText(){ x++; document.querySelector(".text").style.fontSize = x+'px'; console.log(x) } </script></body>
0
0
0
哔哔one
尝试这个 :<!DOCTYPE html><html><head> <title>Page Title</title></head><body> <h1 id='h'>My First Heading</h1> <p id='p'>My first paragraph.</p> <button type='button' id='btn'>increases by 1px</button> <script> document.getElementById("btn").addEventListener("click", function () { var el = document.getElementById('h'); var style = window.getComputedStyle(el, null).getPropertyValue('font-size'); var fontSize = parseFloat(style); el.style.fontSize = (fontSize + 1) + 'px'; }) </script></body></html>
0
0
0
随时随地看视频
慕课网APP
相关分类
JavaScript
我要回答