Using arrays to change color of certain words in an input box

后端 未结 5 771
温柔的废话
温柔的废话 2021-01-21 18:40

So I\'m working on a JSFiddle and I\'m a little confused about something. I have an input box called myTextField with a random paragraph and a button that calls my change functi

5条回答
  •  离开以前
    2021-01-21 19:30

    This is just a guess, but why don't you try this

    This is assuming, you want to change the words, on the blur

    var input = document.getElementById("my-input");
    var par = document.getElementById("par");
    
    var matches = ["every", "most", "that", "half", "much", "the", "another", "her", "my", "their", "a", "an", "his", "neither", "these", "all",
    "its", "no", "this", "any", "those", "both", "least", "our",
    "what", "each", "less", "several", "which", "either", "many", "some",
    "whose", "enough", "more", "such", "your"];
    
    input.addEventListener("blur", function() {
        var inputValue = input.value;
        par.innerHTML = "";
        inputValue.split(' ').forEach(function(word) {
            if (matches.indexOf(word) > -1) {
        	      par.innerHTML += "" + word + " " + "";
            }
            else {
                par.innerHTML += word + " ";
            }
        });
    });
    .colored {
      color: blue;
    }
    
    
    

提交回复
热议问题