gpt4 book ai didi

javascript - InnerHTML 追加项目而不是替换

转载 作者:行者123 更新时间:2023-12-02 16:22:10 27 4
gpt4 key购买 nike

我正在尝试制作一个回文脚本,它实际上正在“工作”。我想做出改进。我希望将输入值附加到我的“输出”div 上。

这是我的 fiddle http://jsfiddle.net/vitorboccio/8yh1u0t7/

有什么提示吗?我不想使用 Jquery !谢谢!

(function () {
"use strict";

var output = document.getElementById("output"),
statusLine = document.getElementById("status"),
phrase = document.getElementById('phrase'),
testButton = document.getElementById("testButton"),
//palindromeText = document.getElementById("palindrome"),
characterCheck = document.getElementById("characterCheck"),
ignoreSpecialCharacters = false,
ignoreSpaces = false;

function setMessage(palindrome) {
if (palindrome) {
output.innerHTML = phrase.value + ' ' + "é palindroma";
} else {
output.innerHTML = phrase.value + ' ' + "não é a palindroma";
}
}

function checkForPalindrome(string) {
var palindrome = true,
right = string.length - 1,
left = 0;

if (!string || string.length < 1) {
// 0 characters
return false;
}

while (left < right && palindrome) {
palindrome = string.charAt(left) === string.charAt(right);
left++;
right--;
}

return palindrome;
}

function executeTest() {
var string = phrase.value,
cleanString;

cleanString = string;

if (ignoreSpaces) {
//ignores whitespaces only;
cleanString = string.replace(/\s+/g, '');
}

if (ignoreSpecialCharacters) {
//ignores punctuation and white space (controversial).
cleanString = string.replace(/[A-Z0-9]/ig, '');
}

if (checkForPalindrome(cleanString)) {
setMessage(true);
palindromeText.innerHTML = '"' + string + '"';
} else {
setMessage(false);
}
}

function executeOnEnter(e) {
if (e.keyCode === 13) {
executeTest();
// phrase.blur();
}
}

//resets the form to state 1
function resetForm() {
output.innerHTML = "";
//statusLine.innerHTML = "Waiting";
statusLine.style.color = "green";
phrase.value = "";
}

function charIgnoreChanged(e) {
ignoreSpecialCharacters = e.target.checked;
}

function spaceIgnoreChanged(e) {
ignoreSpaces = e.target.checked;
}

phrase.addEventListener('keydown', executeOnEnter);
testButton.addEventListener('click', executeTest);
characterCheck.addEventListener('change', charIgnoreChanged);
spaceCheck.addEventListener('change', spaceIgnoreChanged);

}());

最佳答案

你只需要修改setMessage

 function setMessage(palindrome) {
if (palindrome) {
output.innerHTML += phrase.value + ' ' + "é palindroma<br />";
} else {
output.innerHTML += phrase.value + ' ' + "não é a palindroma<br />";
}
// for user convenience, clear the textbox and give it focus
phrase.value = '';
phrase.focus();
}

Fiddle

关于javascript - InnerHTML 追加项目而不是替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29014569/

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