javascript - 如何清空由javascript填充的div标签
问题描述
我有一个脚本,用户在输入框中输入文本,当他们在下面的 div (#Output) 中键入时,它会以不同的字体显示文本。
我的问题是,如果用户删除输入框中的文本,填充的 div 标签留下了所有非动态数据,当没有任何内容时,我怎么能完全清空 div(在这种情况下为#Output)输入。
<script>
$(document).ready(function(){
$("#UpdateText").keyup(function(){
// Getting the current value of textarea
var currentText = $(this).val();
// set font variables
var text = "";
var i;
for (i = 1; i < 30; i++) {
// Setting the font of Div content
text += "<div>Font-" + i + " : <span id=\"fontResult\" style=\"font-family:Font-" + i + "\">" + currentText + "</span></div>";
}
$('#Output').html(text);
});
});
</script>
<div class="content">
<label for="UpdateText">Enter the text you would like here, then select your font</label>
<br />
<textarea id="UpdateText" rows="1" cols="30" style="resize: none; text-align: center" placeholder="Type the name/word here..."></textarea>
<br />
<br />
<div id="Output"></div>
<!-- end .content -->
</div>
解决方案
最快没有任何其他变化:
$('#Output').html(currentText ? text : "");
$(document).ready(function() {
$("#UpdateText").keyup(function() {
// Getting the current value of textarea
var currentText = $(this).val();
// set font variables
var text = "";
var i;
for (i = 1; i < 30; i++) {
// Setting the font of Div content
text += "<div>Font-" + i + " : <span id=\"fontResult\" style=\"font-family:Font-" + i + "\">" + currentText + "</span></div>";
}
$('#Output').html(currentText ? text : "");
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="content">
<label for="UpdateText">Enter the text you would like here, then select your font</label>
<br />
<textarea id="UpdateText" rows="1" cols="30" style="resize: none; text-align: center" placeholder="Type the name/word here..."></textarea>
<br />
<br />
<div id="Output"></div>
<!-- end .content -->
</div>
将版本更新到 ES6 并且不对空字符串执行任何操作
$(function() {
$("#UpdateText").on("input", function() {
$('#Output').empty();
// Getting the current value of textarea
const currentText = $(this).val().trim();
if (currentText) {
// set font variables
const text = [...Array(31).keys()].slice(1)
.map(i => `<div>Font-${i}: <span id="fontResult" style="font-family:Font-${i}">${currentText}</span></div>`);
$('#Output').html(text);
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="content">
<label for="UpdateText">Enter the text you would like here, then select your font</label>
<br />
<textarea id="UpdateText" rows="1" cols="30" style="resize: none; text-align: center" placeholder="Type the name/word here..."></textarea>
<br />
<br />
<div id="Output"></div>
<!-- end .content -->
</div>
推荐阅读
- python-3.x - Pandas:用函数内的空格替换列值
- javascript - 如何使用 javascript for 循环创建一个新数组。我收到错误“圆形对象对象”
- database - 基于 PowerBI 中的多列创建单个数据切片器
- xcode - 应用程序崩溃,但仅在 iOS 14 上,并且仅在通过 MDM 安装时
- javascript - 加载 img src 标签而不是 css 背景
- excel - 按钮单击后UpdatePanel中的Gridview空白
- windows - 代码签名后如何检查软件信誉
- r - 为什么我的 allEffects 图表中的值有误?
- c# - 如何实现一个由鼠标移动的 WPF 控件,如滑块控件但 2D?
- php - 解析点分隔的字符串并创建一个多维数组