[英]How can I keep the font format with this code?
I put up some formatted text on the screen with the following command using inline CSS, but then later I periodically need to change the text with javascript. 我使用内联CSS使用以下命令在屏幕上放置了一些格式化的文本,但是后来我需要定期使用javascript更改文本。 How can I keep the text formatting?
如何保持文本格式? I expect the best way to is to eliminate the inline CSS, but I am not sure how, and even then I don't know how I would maintain the formatting when updating the text.
我希望最好的方法是消除内联CSS,但是我不确定如何,即使那样,我也不知道更新文本时如何保持格式。
array = ["0", "31.2 ℃"]; // Dummy assignment document.getElementById("Out").innerHTML = array[1];
div.Out { font: bold 65px Lora; }
Before: <div class="Out">30.1<span style="font-size:44px">℃</span></div> After: <div id="Out" class="Out">30.1<span style="font-size:44px">℃</span></div>
When I do this, the font blows up. 当我这样做时,字体变大了。
You can get rid of inline css by defining your rules on the <style>
part of your code, or better; 您可以通过在代码的
<style>
部分上定义规则来摆脱内联css,或者更好; by including it on a css file. 通过将其包含在css文件中。
Your array already had a ℃ symbol, I deleted it. 您的数组已经有一个℃符号,我删除了它。
let data = ["0 ℉", "31.2 ℉", "-5 ℃", "36 ℃"]; changeContent(); function changeContent(){ let content = data[randomNumber(data.length)].split(' '); document.getElementById("out").innerHTML = content[0]; document.getElementById("corf").innerHTML = content[1]; } function randomNumber(number){ return Math.floor((Math.random() * number)); }
.out { font: bold 65px Lora; top: 45px; left: 510px; width: 300px; color: black; } .customfontsize{ font-size: 45px; }
<div> <span id="out" class="out"></span> <span id="corf" class="customfontsize"></span> </div> <button onclick="changeContent()">Change content</button>
Edit: added a "change content" button :) 编辑:添加了“更改内容”按钮:)
2nd edit: added a switch function, which will add/remove a class, and then change the innerHTML of the span
if it has the class or not. 第二次编辑:添加了一个switch函数,它将添加/删除一个类,然后更改
span
的innerHTML(如果有或没有该类)。
3rd edit: split temperature and unit. 第三编辑:拆分温度和单位。
OK, so I think the following could work, if I separate the temperature scale into a third array element. 好的,所以我认为,如果我将温度刻度分成第三个数组元素,则可以使用以下方法。 Thank you so much, sodimel!
非常感谢,索迪梅尔!
Edit: It was a bit of a chore creating a third element, because the main code had to be re-arranged in a number of places to accommodate a third, optional element in the page as a whole, but it is working, now. 编辑:创建第三个元素有点麻烦,因为必须在许多地方重新排列主代码,才能在整个页面中容纳第三个可选元素,但现在可以使用了。
let array = ["0", "80.2", "℉"]; // Dummy assignment function changeContent(){ let out = document.getElementById("out"); out.innerHTML = array[1]; let cf = document.getElementById("cf"); cf.innerHTML = array[2]; }
.out { font: bold 65px Lora; top: 45px; left: 510px; width: 300px; color: black; } .customfontsize{ font-size: 45px; }
<div class="out"> <span id="out">30.1</span> <span class="customfontsize" id="cf">℃</span> </div> <button onclick="changeContent()">Change content</button>
Here is one way of separating the numerical value and the degree symbol 这是分离数值和度数符号的一种方法
array = [19.0, 30.1, 31.2]; // Dummy assignment var output = document.getElementById("Out"); var degree = "℃"; // celsius symbol function buttonClick(buttonID){ var newValue; if(buttonID=='b1'){newValue = array[0];} if(buttonID=='b2'){newValue = array[1];} if(buttonID=='b3'){newValue = array[2];} output.innerHTML = "<span class='degreeVal'>"+ newValue +"</span> <span class='degreeType'>"+ degree +"</span>"; }
.degreeVal { font: bold 65px Lora; } .degreeType { font: bold 44px Lora; }
<input type='button' id='b1' value='array value 1' onClick='buttonClick(this.id)'> <input type='button' id='b2' value='array value 2' onClick='buttonClick(this.id)'> <input type='button' id='b3' value='array value 3' onClick='buttonClick(this.id)'> <br><hr><br> Before: <div class="degreeVal">30.2 <span class="degreeType">℃</span> </div> After: <div id="Out" class="degreeVal">0.0 <span class="degreeType"> </span> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.