【问题标题】:search text march Entire text搜索文本行军整个文本
【发布时间】:2022-01-15 11:49:16
【问题描述】:

开发人员。我需要帮助。我在下面给出了代码。当我在搜索框中键入任何内容时,键入的值与列表项的开头匹配。但是我希望当我在搜索框中键入任何内容时,键入值以匹配搜索项的任何部分,并且文本颜色将为红色。我尝试了很多次。

function myFunction(e) {
  // find all `li` > a elements
  let col=document.querySelectorAll('ul#myUL li a');
      // iterate through all elements; re-hide & remove className from each.
      col.forEach(n=>{
      
        n.parentNode.style.display='none';
        n.classList.remove('bold');
        
        // if the typed value matches the beginning of a list item; display the text & assign Bold className
        if( this.value.length > 0 && this.value.trim()!='' && n.textContent.toLowerCase().startsWith( this.value.toLowerCase() ) ){
        
          n.parentNode.style.display='block';
          // make the whole word bold
          //n.classList.add('bold');
          
          // make the matched portion bold
          n.innerHTML = `<span class="bold">${this.value}</span>` + n.textContent.substr(this.value.length)
        }
      });
}



document.querySelector('input[name="search"]').addEventListener('keyup',myFunction);
* {
  box-sizing: border-box;
}

#myInput {
  background-image: url('/css/searchicon.png');
  background-position: 10px 12px;
  background-repeat: no-repeat;
  width: 100%;
  font-size: 16px;
  padding: 12px 20px 12px 40px;
  border: 1px solid #ddd;
  margin-bottom: 12px;
}

#myUL {
  list-style-type: none;
  padding: 0;
  margin: 0;
}
#myUL li{
  display:none;
}

#myUL li a {
  border: 1px solid #ddd;
  margin-top: -1px; /* Prevent double borders */
  background-color: #f6f6f6;
  padding: 12px;
  text-decoration: none;
  font-size: 18px;
  color: black;
  display: block
}

#myUL li a:hover:not(.header) {
  background-color: #eee;
}

.bold{font-weight:bold;color:red
<input name='search' type="text" placeholder="Search for names.." title="Type in a name" />
<ul id="myUL">
  <li><a href="#">Adele</a></li>
  <li><a href="#">Agnes</a></li>
  <li><a href="#">Billy</a></li>
  <li><a href="#">Bob</a></li>
  <li><a href="#">Calvin</a></li>
  <li><a href="#">Christina</a></li>
  <li><a href="#">Cindy</a></li>
</ul>

【问题讨论】:

    标签: javascript html css


    【解决方案1】:

    使用includes() 代替startsWith()。要显示找到的文本,您需要显示之前的文本以及之后的文本。

    function myFunction(e) {
      // find all `li` > a elements
      let col=document.querySelectorAll('ul#myUL li a');
          // iterate through all elements; re-hide & remove className from each.
          col.forEach(n=>{
          
            n.parentNode.style.display='none';
            n.classList.remove('bold');
            
            // if the typed value matches the beginning of a list item; display the text & assign Bold className
            if( this.value.length > 0 && this.value.trim()!='' && n.textContent.toLowerCase().includes( this.value.toLowerCase() ) ){
            
              n.parentNode.style.display='block';
              // make the whole word bold
              //n.classList.add('bold');
              
              // make the matched portion bold
              n.innerHTML = n.textContent.substr(0,n.textContent.toLowerCase().indexOf(this.value.toLowerCase()))+`<span class="bold">${this.value}</span>` + n.textContent.substr(n.textContent.toLowerCase().indexOf(this.value.toLowerCase())+this.value.length)
            }
          });
    }
    
    
    
    document.querySelector('input[name="search"]').addEventListener('keyup',myFunction);
    * {
      box-sizing: border-box;
    }
    
    #myInput {
      background-image: url('/css/searchicon.png');
      background-position: 10px 12px;
      background-repeat: no-repeat;
      width: 100%;
      font-size: 16px;
      padding: 12px 20px 12px 40px;
      border: 1px solid #ddd;
      margin-bottom: 12px;
    }
    
    #myUL {
      list-style-type: none;
      padding: 0;
      margin: 0;
    }
    #myUL li{
      display:none;
    }
    
    #myUL li a {
      border: 1px solid #ddd;
      margin-top: -1px; /* Prevent double borders */
      background-color: #f6f6f6;
      padding: 12px;
      text-decoration: none;
      font-size: 18px;
      color: black;
      display: block
    }
    
    #myUL li a:hover:not(.header) {
      background-color: #eee;
    }
    
    .bold{font-weight:bold;color:red
    <input name='search' type="text" placeholder="Search for names.." title="Type in a name" />
    <ul id="myUL">
      <li><a href="#">Adele</a></li>
      <li><a href="#">Agnes</a></li>
      <li><a href="#">Billy</a></li>
      <li><a href="#">Bob</a></li>
      <li><a href="#">Calvin</a></li>
      <li><a href="#">Christina</a></li>
      <li><a href="#">Cindy</a></li>
    </ul>

    【讨论】:

    • 通过执行以下n.textContent.replace(this.value, `&lt;span style="color: red;"&gt;${this.value}&lt;/span&gt;`),用于突出显示搜索查询的代码可以更加简洁
    【解决方案2】:

    在 Javascript 中有一个字符串方法includes(),如果子字符串存在于特定字符串中则返回 true,否则返回 false。

    以下代码将仅列出包含用户输入的查询的那些项目,并突出显示该列表项中存在搜索查询的位置

      function myFunction(e) {
        // find all `li` > a elements
        let col = document.querySelectorAll('ul#myUL li a');
        // iterate through all elements; re-hide & remove className from each.
        col.forEach(n => {
    
          n.parentNode.style.display = 'none';
          n.classList.remove('bold');
    
          // if the typed value matches the content of a list item; display the text & assign Bold className
          if (n.textContent.toLowerCase().includes(this.value.toLowerCase())) {
            console.log(n.textContent)
            n.parentNode.style.display = 'block';
            
            n.innerHTML = `${n.textContent.replace(this.value, `<span style="color: red;">${this.value}</span>`)}`
          }
        });
      }
    
    
    
      document.querySelector('input[name="search"]').addEventListener('keyup', myFunction);
    

    【讨论】:

      猜你喜欢
      • 2018-06-19
      • 1970-01-01
      • 2016-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多