javascript - 如何使用javascript从下拉菜单中将默认输出文本设置为选定的选项
问题描述
我正在为我的网站创建一个自定义字体预览框(我是一名字体设计师,希望我的客户能够在购买前试用字体)并且我已经使用 javascript、css 和html。整个事情都有效,但我遇到了一个问题。
该框的工作原理如下:
- 顶部有一个带有占位符文本的文本区域,客户可以在其中输入他或她自己的文本
- 有一个字体下拉菜单可供选择(其中之一是通过 html “选择”)和一个文本框来更改字体大小
- 然后在这些下方,有一行输出显示占位符或客户键入的文本。
我遇到的问题是我希望输出从一开始就以“选定”字体显示,而是以网站的默认字体显示。在我取消选择并在下拉菜单中重新选择该字体之前,它不会更改为所选字体。
我可以使用标签在 html 中设置它,但这只会影响占位符文本。如果客户在框中键入任何内容,它会立即更改回默认字体,并且客户必须取消选择并重新选择下拉菜单中的字体以更改输出。所以这很令人困惑。
这是 JSBin 链接:https ://jsbin.com/lenutufibu/2/edit?html,css,js,output
这是我使用的代码:
<h2>Test-Drive This Font</h2>
<div class="fontForm">
<form method="POST">
<textarea name="customText" placeholder="Type your own text here and it will preview below." id="customText" cols="80" rows="1"></textarea>
<div class="font-third">
Font Size
<br>
<input type="text" placeholder="24" id="customSize">
</div>
<div class="font-third">
Select Font <select name="customFont" id="customFont">
<option value="flatline-regular" id="flatline-regular" selected>Flatline Regular</option>
<option value="arial" id="arial">Arial</option>
</select>
</div>
</form>
敏捷的棕色狐狸跳过了懒狗。
然后是我的示例中使用的字体的 CSS:
@font-face {
font-family: 'flatline-regular';
src: url(https://static1.squarespace.com/static/5579e750e4b06072e5c3004a/t/5e14be111ff9ed15ac0b5f31/1578417681898/Flatline-Regular.otf), url(fontURL.woffhttps://static1.squarespace.com/static/5579e750e4b06072e5c3004a/t/5e14be2a1ff9ed15ac0b6230/1578417707009/flatline-regular-webfont.woff), url(fontURL.woff2https://static1.squarespace.com/static/5579e750e4b06072e5c3004a/t/5e14be31e375184c7b041567/1578417713132/flatline-regular-webfont.woff2);
}
和 javascript(使用https://code.jquery.com/jquery-3.1.0.js):
$(document).ready(() => {
$('#customText').on('keyup', event => {
$('.font-preview').html($(event.currentTarget).val());
});
$('#customFont').on('change', event => {
$('.font-preview').css('fontFamily', $(event.currentTarget).val());
});
$('#customSize').on('keyup', event => {
var fontSize = ($(event.currentTarget).val()) + 'px';
$('.font-preview').css('fontSize', fontSize);
});
})
解决方案
您只需要在 DOM 就绪事件中设置选定的字体,可以这样完成:
$('.font-preview').css('fontFamily', $("#customFont").val());
请参阅下面的工作代码
$(document).ready(() => {
$('.font-preview').css('fontFamily', $("#customFont").val());
$('#customText').on('keyup', event => {
$('.font-preview').html($(event.currentTarget).val());
});
$('#customFont').on('change', event => {
$('.font-preview').css('fontFamily', $(event.currentTarget).val());
});
$('#customSize').on('keyup', event => {
var fontSize = ($(event.currentTarget).val()) + 'px';
$('.font-preview').css('fontSize', fontSize);
});
})
@font-face {
font-family: 'flatline-regular';
src: url(https://static1.squarespace.com/static/5579e750e4b06072e5c3004a/t/5e14be111ff9ed15ac0b5f31/1578417681898/Flatline-Regular.otf), url(fontURL.woffhttps://static1.squarespace.com/static/5579e750e4b06072e5c3004a/t/5e14be2a1ff9ed15ac0b6230/1578417707009/flatline-regular-webfont.woff), url(fontURL.woff2https://static1.squarespace.com/static/5579e750e4b06072e5c3004a/t/5e14be31e375184c7b041567/1578417713132/flatline-regular-webfont.woff2);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="font-box-container">
<h2>Test-Drive This Font</h2>
<div class="fontForm">
<form method="POST">
<textarea name="customText" placeholder="Type your own text here and it will preview below." id="customText" cols="80" rows="1"></textarea>
<div class="font-third">
Font Size
<br>
<input type="text" placeholder="24" id="customSize">
</div>
<div class="font-third">
Select Font
<select name="customFont" id="customFont">
<option value="flatline-regular" id="flatline-regular" selected>Flatline Regular</option>
<option value="arial" id="arial">Arial</option>
</select>
</div>
</form>
</div>
<div class="font-preview">The quick brown fox jumps over the lazy dog.</div>
</div>
推荐阅读
- java - 使用方法而不是 @annotation 的 Spring AOP 建议:为什么?
- mysql - 当 useInformationSchema 为真时,为什么 tinyint(1) 不被视为布尔值?
- react-native - 流程如何与本机反应和选项'all = true'一起使用?
- c# - 如何在调试应用程序时打开/编辑/保护本地 app.config 文件?
- delphi - Delphi cxGrid 如何防止在移动列时创建第二行标题?
- python - Python Regex:从字符串中解析键/值对
- laravel - Laravel s3存储:未定义的方法'url'
- python - 包含多个 np.multipy 语句的代码片段优化
- android - 如何将耀斑动画从几秒钟减慢到几小时
- xamarin - Xamarin.Forms iOS 添加联系人(通过依赖服务)