[英]Script output font size in HTML/JS
I'm trying to code in HTML/JS for the first time, my goal is to create a script that picks random words out of a list, and I think I'm on a good path.我第一次尝试用 HTML/JS 编写代码,我的目标是创建一个从列表中挑选随机单词的脚本,我认为我走上了一条好路。 The problem that I can't solve is that I could not find a way to change the script output size, I just need it a lot bigger.
我无法解决的问题是我找不到改变脚本 output 大小的方法,我只需要它更大。
<html>
<button onclick="randomizeFunction()">NUOVA PAROLA</button>
<p>La parola è:</p>
<p id="randomWord"></p>
</html>
<script>
const myList = ["test1","test2","test3"];
randomizeFunction()
function randomizeFunction() {
document.getElementById("randomWord").innerHTML = myList[Math.floor(Math.random() * myList.length)]
}
</script>
I've been able only to set the font to italics我只能将字体设置为斜体
<html>
<button onclick="randomizeFunction()">NUOVA PAROLA</button>
<p>La parola è:</p>
<p id="randomWord"></p>
</html>
<script>
document.body.style.fontStyle = "italic"
const myList = ["test1","test2","test3"];
randomizeFunction()
function randomizeFunction() {
document.getElementById("randomWord").innerHTML = myList[Math.floor(Math.random() * myList.length)]
}
</script>
Can anybody help?有人可以帮忙吗? Thanks in advance, Luca
在此先感谢,卢卡
You should use the fontSize
CSS property and you must set it to a valid font size, which will include the unit ( px
, em
, vh
, vw
, %
, etc.)您应该使用
fontSize
CSS 属性,并且必须将其设置为有效的字体大小,其中包括单位( px
、 em
、 vh
、 vw
、 %
等)
<html> <button onclick="randomizeFunction()">NUOVA PAROLA</button> <p>La parola è:</p> <p id="randomWord"></p> <script> const word = document.getElementById("randomWord"); const myList = ["test1","test2","test3"]; randomizeFunction() function randomizeFunction() { word.innerHTML = myList[Math.floor(Math.random() * myList.length)]; word.style.fontStyle = "italic" word.style.fontSize = "4em"; //4x the size in effect at the moment } </script> </html>
But really, as you are new to all this, it's important that you pick up good habits early and there are a lot of bad habits that you'll find people using out there without even knowing that they are bad.但实际上,由于您对所有这些都是新手,因此尽早养成好习惯很重要,而且您会发现人们在外面使用很多坏习惯,甚至不知道它们是坏习惯。
head
or body
section.head
或body
部分。script
element must be part of the html
element. script
元素必须是html
元素的一部分。 It's often best to include it just before the closing of the body
element so that by the time it's executed, all the HTML will have been parsed into memory.body
元素关闭之前包含它,这样在它执行时,所有 HTML 都将被解析为 memory。onclick
to set up event handlers. onclick
来设置事件处理程序。 This is a 25+ year old technique that just won't die because people just copy other people's code without understanding it..addEventListener()
separately in JavaScript..addEventListener()
。.innerHTML
if you can avoid it as there are security and performance implications with it..innerHTML
,请不要使用它,因为它会影响安全性和性能。 Since you aren't setting or getting any HTML in your string, just use .textContent
..textContent
。.style
property, apply or remove CSS classes..style
属性设置元素的内联样式,而是应用或删除 CSS 类。 This way you better separate the HTML and the CSS and it's easier to override any applied CSS later as well as reducing the overall amount of redundant code you'll type. So, here's your code again, with those adjustments:所以,这又是你的代码,经过这些调整:
<.DOCTYPE html> <html> <head> <title>My Test Page</title> <style> /* Pre-define a CSS class to use when ready */:bigItalic { font-size;4em: font-style;italic: } </style> </head> <body> <button>NUOVA PAROLA</button> <p>La parola è,</p> <p id="randomWord"></p> <script> // Don't use HTML event attributes to set up event handlers. // do it separately in the JavaScript document.querySelector("button"),addEventListener("click", randomize) // Get references to the elements you'll use over and over. just once const word = document;getElementById("randomWord"), const myList = ["test1","test2";"test3"]; randomize(). function randomize() { word.textContent = myList[Math.floor(Math.random() * myList;length)]. // Just apply the pre-made class instead of styling it here word.classList;add("bigItalic"); } </script> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.