1 回答
TA贡献1873条经验 获得超9个赞
您希望系统在文本区域为空时发出警告。您可以通过将 If-else 条件表达式添加到代码中来执行此操作。
const displayText = () => {
const inputPage = document.getElementById("input-page");
const countPage = document.getElementById("count-page");
const inputtedText = document.getElementById("inputted-text");
const inputtedTextValue = inputtedText.value;
if (inputtedText.value !== "") { // normal flow will continue if the text-area is not empty
inputPage.style.display = "none";
document.getElementById("display-user-text").innerText = inputtedTextValue;
countPage.style.display = "block";
} else { // if the text-area is empty, it will issue a warning.
alert("this area required")
}
console.log(countWords(inputtedTextValue));
};
const countWords = (str) => {
return str.split(" ").length;
};
const renderWordCount = () => {
document.getElementById("word-count") = wordCount;
};
<div id="input-page">
<h1>Word Counter</h1>
<form action="">
<textarea id="inputted-text" type="text" rows="22" cols="60"></textarea>
<br />
</form>
<button onclick="displayText()">COUNT</button>
</div>
<div id="count-page" style="display: none;">
<h1>Your Text:</h1>
<p id="display-user-text"></p>
<div>
<h1 id="word-count">Count: </h1>
</div>
</div>
添加回答
举报