【问题标题】:Change background-color of each word in search bar更改搜索栏中每个单词的背景颜色
【发布时间】:2016-07-24 06:18:17
【问题描述】:

我一直在尝试对 jQuery 中的每个单词进行攻击并给它们一个彩色背景,我找到了一个解决方案,但它只适用于 html 内容,我想为搜索栏的输入着色。这是我发现的一个例子

HTML

some content
<div>another content 
    <input id="search" type="text" value="dsfdsfdsf sdf dsfsdfsfs">
</div>
<p>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse ac turpis
</p>
content in body

jQuery

//the main point here is you need to replace only the contents of the text node, not all the html parts

var colours = ['red', 'green', 'blue', 'cyan', 'magenta', 'yellow', 'gray'];
var lastC;

jQuery('*').contents().each(function () {
    //not a text node or there is no content to replace
    if (this.nodeType != 3 || !this.nodeValue.trim()) {
        return;
    }

    //replace the value of the text node
    $(this).replaceWith(this.nodeValue.replace(/\w+/g, function (part) {
        var c = colours[Math.floor(Math.random() * colours.length)];
        while (c == lastC) {
            var c = colours[Math.floor(Math.random() * colours.length)];
        }
        lastC = c;
        return '<span style="background-color: '+c+';">' + part.split("").join("") + '</span>'
    }))
});

我尝试将jQuery('*') 更改为jQuery('#search'),但没有成功。

实时示例 https://jsfiddle.net/7hs9mgLp/2/

我该怎么做才能解决它?

【问题讨论】:

    标签: jquery html css search highlight


    【解决方案1】:

    事实上,它不能与文本输入元素一起使用,因为该函数获取 content 以将其包含在 span 元素中,而不是 value 的元素。

    但我已经进行了修复以使其适用于您的代码,并在 background input 元素上应用随机 color

    You can see it here

    这是 JS 代码:

    //the main point here is you need to replace only the contents of the text node, not all the html parts
    
    var colours = ['red', 'green', 'blue', 'cyan', 'magenta', 'yellow', 'gray'];
    var lastC;
    
    jQuery('*').contents().each(function () {
        //not a text node or there is no content to replace
         // here is the part I've changed. I test what is the element we get
         if ( $(this).is( "input" ) ) {
            color = colours[Math.floor(Math.random() * colours.length)];
            $(this).css('background-color',color);
         }
        else{
          if (this.nodeType != 3 || !this.nodeValue.trim()) {
              return;
          }
    
          //replace the value of the text node
          $(this).replaceWith(this.nodeValue.replace(/\w+/g, function (part) {
              var c = colours[Math.floor(Math.random() * colours.length)];
              while (c == lastC) {
                  var c = colours[Math.floor(Math.random() * colours.length)];
              }
              lastC = c;
              return '<span style="background-color: '+c+';">' + part.split("").join("") + '</span>'
          }));
        }
    });
    

    编辑

    要为输入中的每个单词着色,有点棘手,我不知道是否有“干净”的方法来做到这一点。

    无论如何,这是我对这部分的建议

    See this updated fiddle

    我已经改变了一些 HTML 结构来做到这一点,因为我想不出另一种方法来做到这一点。希望对您有所帮助。

    这里是更新的 JS 部分:

    if ( $(this).is( "input" ) ) {
      val = $(this).val().split(" ");
      $( val ).each(function(i,v ) {
        color = colours[Math.floor(Math.random() * colours.length)];
        $("label").append("<span class='absolute' style='background-color: "+color+"; '>"+v+"</span>");
        $('#search').css('position','relative');
        $(this).val('');
        $('label').css({'position' : 'absolute', 'left' : '2px', 'top' : '2px' });
      });
    }
    

    【讨论】:

    • 谢谢,你给了我解决方案的想法,但我不想整个页面都被着色我希望搜索栏中的每个单词都有不同的背景颜色而不是整个背景像你一样。谢谢
    • 这有点棘手,但我找到了一种可能的方法来获得你想要的结果。我已经更新了我的答案。
    • 非常感谢,我现在就可以正常工作了。我不知道如何感谢你对猪的帮助。我真的很感激。谢谢。
    • 不客气,我喜欢这个,因为我以前从来没有做过这样的事情:)
    猜你喜欢
    • 2018-07-31
    • 2016-02-18
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多