gpt4 book ai didi

javascript - 通过选项下拉 Javascript 更改字体样式

转载 作者:行者123 更新时间:2023-12-02 21:01:21 25 4
gpt4 key购买 nike

我有一个测试,使用 Javascript 和 CSS 来更改组合框的文本样式。但是,我只是发现这个网站的字体类型发生了变化,我从谷歌那里什么也没得到。这是我的代码:

var changeFont = function(font) {
console.log(font.value)
document.getElementById("output-text").style.fontFamily = font.value;
}
.tebal {
font-family: Verdana;
font-size: 12px;
color: blue;
font-weight: bold;
}

.miring {
font-family: Verdana;
font-size: 12px;
color: blue;
font-weight: italic;
}
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="windows-1252">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js”></script>
<script src="http://code.jquery.com/jquery-git2.js"></script>

<div class="jumbotron text-center">
<h3>Text Style Changed with combobox</h3>
</div>

<div class="text-center" id="output-text">
Hello Guys
</div>

<div class="text-center">
<select id="input-font" class="input" onchange="changeFont (this);">
<option value="Times New Roman" selected="selected">Times New Roman</option>
<option value="Arial">Arial</option>
<option value="algerian">Algerian</option>
<option value="berlin sans fb">Berlin Sans FB</option>
<option value="bold" class="bold">Tebal</option>
<option value="miring" class="miring">Miring</option>
<option value="" class="">Style1</option>
<option value="" class="">Style2</option>
<option value="" class="">Style3</option>
</select>
</div>

我想让它像这样This (Photo)每次我从组合框更改样式,样式文本都会更改,但我找不到如何制作它的示例以及我的代码有什么问题。请帮我解决这个代码:(,谢谢。

注意:这是我照片的翻译 - ini 文本 berubah sesuai css = 此文本根据 css 更改 - tebal = 粗体 - Miring = 斜体 - bingkai=框架

最佳答案

我认为为每种字体定义类会更简单。然后将类名放入选项值中。有了这个,如果您需要向文本添加其他样式(不仅是字体系列,还可以向类添加颜色或其他任何内容),它将更加灵活这是您的案例的示例

var changeFont = function(font) {
console.log(font.value)
document.getElementById("output-text").className = "text-center " + font.value;
}
.bold {
font-family: Verdana;
font-size: 12px;
color: blue;
font-weight: bold;
}

.miring {
font-family: Verdana;
font-size: 12px;
color: blue;
font-style: italic;
}

.Arial{
font-family: Arial;
}
.Algerian{
font-family: Algerian;
}
.Berlin-sans-fb{
font-family: 'Berlin sans fb';
}
.Times-new-roman{
font-family: 'Times New Roman';
}
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="windows-1252">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js”></script>
<script src="http://code.jquery.com/jquery-git2.js"></script>
</head>

<body>
<div class="jumbotron text-center">
<h3>Text Style Changed with combobox</h3>
</div>

<div class="text-center" id="output-text">
Hello Guys
</div>

<div class="text-center">
<select id="input-font" class="input" onchange="changeFont (this);">
<option value="Times-new-roman" selected="selected">Times New Roman</option>
<option value="Arial">Arial</option>
<option value="Algerian">Algerian</option>
<option value="Berlin-sans-fb">Berlin Sans FB</option>
<option value="bold" class="bold">Tebal</option>
<option value="miring" class="miring">Miring</option>
<option value="" class="">Style1</option>
<option value="" class="">Style2</option>
<option value="" class="">Style3</option>
</select>
</div>

关于javascript - 通过选项下拉 Javascript 更改字体样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61346313/

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