Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JavaScript中创建将文本增加1px的按钮_Javascript_Html_Css_Button - Fatal编程技术网

如何在JavaScript中创建将文本增加1px的按钮

如何在JavaScript中创建将文本增加1px的按钮,javascript,html,css,button,Javascript,Html,Css,Button,我需要你的帮助。我是JavaScript的初学者,我想创建一个按钮,将文本增加1px。你能帮我吗?这应该会有帮助的窗口.getComputedStyle(txt,null).getPropertyValue('font-size')这是可以用来获取元素的fontSize const txt=document.querySelector(“#txt”); 函数changeFontSize(){ 让fontSize=window.getComputedStyle(txt,null).getProp

我需要你的帮助。我是JavaScript的初学者,我想创建一个按钮,将文本增加1px。你能帮我吗?

这应该会有帮助的<代码>窗口.getComputedStyle(txt,null).getPropertyValue('font-size')这是可以用来获取元素的
fontSize

const txt=document.querySelector(“#txt”);
函数changeFontSize(){
让fontSize=window.getComputedStyle(txt,null).getPropertyValue('font-size');
设newFontSize=Number(fontSize.slice(0,2))+1
txt.style.fontSize=`${newFontSize}px`;
}
#txt{
字体大小:16px;
}
示例文本

单击
正如上面的Som replay一样,有很多方法可以做到这一点

我的方式

<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>

你好,艾哈迈德

点击我 x=1; document.querySelector(“.btn”).addEventListener(“单击”,函数(){ 增量文本(); }) 函数increseText(){ x++; document.querySelector(“.text”).style.fontSize=x+'px'; console.log(x) }
试试这个:


页面标题
我的第一个标题

我的第一段

增加1倍 document.getElementById(“btn”).addEventListener(“单击”,函数)(){ var el=document.getElementById('h'); var style=window.getComputedStyle(el,null).getPropertyValue('font-size'); var fontSize=parseFloat(样式); el.style.fontSize=(fontSize+1)+“px”; })
添加一些您拥有的代码和html标记,以帮助我们帮助您)