如何使用 javascript 将默认输出文本设置为从下拉菜单中选择的选项
How to set default output text to selected option from dropdown menu using javascript
我正在为我的网站创建一个自定义字体预览框(我是一名字体设计师,希望我的客户能够在购买前试用字体)并且我已经使用尽可能简单的方式将其全部编码javascript、css 和 html。一切正常,但我遇到了一个问题。
盒子的工作原理如下:
- 顶部有一个带有占位符文本的文本区域,其中
客户可以输入自己的文字
- select 有一个字体下拉菜单(其中之一是
"selected" 通过 html) 和一个文本框来更改字体大小
- 然后在这些下面,有一行输出显示
占位符或客户键入的文本。
我遇到的问题是我希望输出从一开始就以 "selected" 字体显示,而是以网站的默认字体显示。它不会更改为 selected 字体,直到我在下拉菜单中取消 select 并重新 select 该字体。
我可以使用标签在 html 中进行设置,但这只会影响占位符文本。如果客户在框中键入任何内容,它会立即变回默认字体,并且客户必须取消 select 并重新 select 下拉列表中的字体才能更改输出。所以这很令人困惑。
这是 JSBin link: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>
在 $(document).ready(() => {
下方添加 $('.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);
}
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="custom-font-previewer">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
</head>
<body>
<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>
</body>
</html>
我正在为我的网站创建一个自定义字体预览框(我是一名字体设计师,希望我的客户能够在购买前试用字体)并且我已经使用尽可能简单的方式将其全部编码javascript、css 和 html。一切正常,但我遇到了一个问题。
盒子的工作原理如下:
- 顶部有一个带有占位符文本的文本区域,其中 客户可以输入自己的文字
- select 有一个字体下拉菜单(其中之一是 "selected" 通过 html) 和一个文本框来更改字体大小
- 然后在这些下面,有一行输出显示 占位符或客户键入的文本。
我遇到的问题是我希望输出从一开始就以 "selected" 字体显示,而是以网站的默认字体显示。它不会更改为 selected 字体,直到我在下拉菜单中取消 select 并重新 select 该字体。
我可以使用标签在 html 中进行设置,但这只会影响占位符文本。如果客户在框中键入任何内容,它会立即变回默认字体,并且客户必须取消 select 并重新 select 下拉列表中的字体才能更改输出。所以这很令人困惑。
这是 JSBin link: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>
在 $(document).ready(() => {
下方添加 $('.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);
}
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="custom-font-previewer">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
</head>
<body>
<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>
</body>
</html>