0

идея кода в том, чтобы когда есть совпадение в input-е и в массиве то тот участок текста должен подсвечиватся красным. Вопрос, как изменить цвет содержимого массива в js?

let x = document.querySelector(".p").innerHTML  
let z = x.split(" ")
let y = document.querySelector(".input")
y.oninput = function(){
    for (let i = 0; i < x.length; i++) {
        if(y.value == z[i]){
            //Код для подсвечивании
        }
    }
<input type="text" class = "input">
    <p class="p">
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>

3
  • при запуске сниппета пишет: "message": "Uncaught SyntaxError: Unexpected end of input", 9 янв 2021 в 9:56
  • Ну ведь задавали же вопрос этот неделю назад
    – DiD
    9 янв 2021 в 10:48
  • это другой вопрос 9 янв 2021 в 10:53

1 ответ 1

1

У меня получилось такое решение:

 window.onload= function(){
    let x = document.getElementsByClassName('p')[0].innerHTML;  
    let y = document.getElementsByClassName('input')[0];
    y.oninput = function(){
        if(y.value.length != 0){
        var newstr = "";
          for (let i = 0; i < x.length; i++) {
              var nextsim = "";

              for(var XX=0;XX<y.value.length;XX++){
                if(!!x[i+XX]){
                  nextsim = nextsim + x[i+XX];
                  
                }

              }
              
              if(y.value == nextsim){
                newstr = newstr + '<span style="color: red">'+nextsim+'</span>';
                i = i + y.value.length-1;
              }else{
                newstr = newstr + x[i];
                
              }
          }
          
          document.getElementsByClassName('p')[0].innerHTML = newstr;
        }else{ document.getElementsByClassName('p')[0].innerHTML = x;}
      }
    }
<input type="text" class = "input">
    <p class="p">
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.