简体   繁体   English

InnerHTML附加项,而不是替换项

[英]InnerHTML Append Item instead of Replace

I'm trying to make a palindrome script and it's actually "working". 我正在尝试编写回文脚本,并且实际上是“有效的”。 I want to make a improvement. 我要改善。 I want the input value append on my "output" div. 我希望将输入值附加在“输出” div上。

here are my fiddle http://jsfiddle.net/vitorboccio/8yh1u0t7/ 这是我的小提琴http://jsfiddle.net/vitorboccio/8yh1u0t7/

any hints ? 有什么提示吗? I dont want to use Jquery ! 我不想使用Jquery! Thanks! 谢谢!

(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);

}());

You can append to the div by keeping the original innerHTML like this: 您可以通过如下方式保留原始的innerHTML来追加到div:

output.innerHTML = output.innerHTML + "<br />" + phrase.value + ' ' + "é palindroma";

or shorter: 或更短:

output.innerHTML += "<br />" + phrase.value + ' ' + "é palindroma";

You just need to modify setMessage 您只需要修改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 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM