gpt4 book ai didi

javascript - 在 JavaScript 中格式化 document.write() 的输出

转载 作者:行者123 更新时间:2023-12-03 08:32:29 25 4
gpt4 key购买 nike

我创建了一个简单的计算器,它可以根据您在类里面获得的分数告诉您您获得的字母等级。然而,我想做的是格式化 document.write() 的文本。我希望它不只是简单地打印文本,而是打印出具有特定大小字体的文本、网页内特定背景颜色的文本(就在 document.write() 的输出周围),并且我还想在生成的输出下划线。

<html>
<head><title>Letter-Grade Calculator</title></head>
<body>
<h3>Letter-Grade Calculator</h3>
<script type="text/javascript">

var score = parseFloat(prompt ("What is your score?"));

if (score >= 90){
document.write("The score you entered is: " + score + ". Your letter grade is: A!");
}
else if (score >= 80 && score <= 89.9){
document.write("The score you entered is: " + score + ". Your letter grade is: B!");
}
else if (score >= 70 && score <= 79.9){
document.write("The score you entered is: " + score + ". Your letter grade is: C!");
}
else if (score >= 60 && score <= 69.9){
document.write("The score you entered is: " + score + ". Your letter grade is: D!");
}
else {
document.write("The score you entered is: " + score + ". Your letter grade is: F!");
}

</script>
</body>
</html>

我厌倦了将每个 document.write() 中的文本分配给特定变量,然后在 document.write() 中调用该变量(例如:假设我有一个变量“gradeA”,然后设置第一个 if 条件返回“document.write(gradeA);”(如果为 true)),然后使用我在网上找到的某些扩展来尝试修改变量。但我无法让它输出我需要的内容。

最佳答案

使用CSS和标签...

<html>
<head><title>Letter-Grade Calculator</title></head>
<body>
<h3>Letter-Grade Calculator</h3>
<style>
.red {background: red;};
.blue {background: blue;};
.green {background: green;};
.yellow {background: yellow;};
.black {background: black;};
</style>
<script type="text/javascript">

var score = parseFloat(prompt ("What is your score?"));

if (score >= 90){
document.write("<span class='red'>The score you entered is: " + score + ". Your letter grade is: A!</span>");
}
else if (score >= 80 && score <= 89.9){
document.write("<span class='green'>The score you entered is: " + score + ". Your letter grade is: B!</span>");
}
else if (score >= 70 && score <= 79.9){
document.write("<span class='blue'>The score you entered is: " + score + ". Your letter grade is: C!</span>");
}
else if (score >= 60 && score <= 69.9){
document.write("<span class='yellow'>The score you entered is: " + score + ". Your letter grade is: D!</span>");
}
else {
document.write("<span class='black'>The score you entered is: " + score + ". Your letter grade is: F!</span>");
}

</script>
</body>
</html>

关于javascript - 在 JavaScript 中格式化 document.write() 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33286610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com