簡體   English   中英

如何使用數學方法來設置元素的樣式?

[英]How can I use a Math method to style an element?

我試圖給元素一個 1 - 100% 的隨機寬度,但這不起作用。

  document.querySelector(".box").style.width = Math.floor((Math.random() * 100) + 1)%;

您需要將%連接為字符串:

document.querySelector(".box").style.width = Math.floor((Math.random() * 100) + 1) + '%';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM