简体   繁体   English

在聊天框中突出显示关键字,如何为几个关键字添加不同的样式?

[英]highlight keywords in a chatbox, how to add different styles to several keywords?

I'm editing a userscript that has a highlight function inside a chat box on a gaming site. 我正在编辑一个在游戏网站的聊天框中具有突出显示功能的用户脚本。 I'm trying to make it so I can add keywords and different css styles to it. 我正在尝试使其能够为它添加关键字和不同的CSS样式。 I'm looking for something that adds keywords into groups, so that I can make one style to several keywords in that group. 我正在寻找将关键字添加到组中的内容,以便可以为该组中的多个关键字制作一种样式。 And a different style to another group. 和另一组的风格不同。

This is how it looks like 看起来像这样

http://i.imgur.com/7nbMuA4.png http://i.imgur.com/7nbMuA4.png

dctools.checkForChatKeyword = function(elements) {
    if (GM_getValue("dct-options-hiflags") != null) {
        $(elements).highlight('flag')

            .highlight('keyword 1') 
            .highlight('keyword 2')
            .highlight('keyword 3')
            .highlight('keyword 4')
            .highlight('keyword 5')
            .highlight('keyword 6')
            .highlight('keyword 7');
    }

Is there a simple way to add css styles to these different keywords using arrays? 有没有一种简单的方法可以使用数组向这些不同的关键字添加CSS样式?

In general , (since you don't show the .highlight function code) you would: 通常 ,(因为您没有显示.highlight函数代码)您将:

  1. Create a series of jQuery style objects, like: 创建一系列jQuery样式对象,例如:

     var styleRedOnYellow = {"color": "red", "background": "yellow"} , styleWhiteOnBlack = {"color": "white", "background": "black"} , styleLimeOnPink = {"color": "lime", "background": "pink"} , stylePuke_1 = {"color": "#EEF272", "background": "#97CC9A"} ; 
  2. Then you can link keywords with styles using an associative array : 然后,您可以使用关联数组将关键字与样式链接起来

     var stylePerKeyword = []; stylePerKeyword["keyword 1"] = styleRedOnYellow; stylePerKeyword["apple"] = styleRedOnYellow; stylePerKeyword["keyword 3"] = styleLimeOnPink; stylePerKeyword["keyword 4"] = styleLimeOnPink; stylePerKeyword["shoes"] = stylePuke_1; 
  3. Then inside the .highlight function, it can apply the styles to the wrapping nodes something like so: 然后在.highlight函数中,它可以将样式应用于包装节点,如下所示:

     /*--- keywordParameter is the variable inside highlight() that contains the particular keyword for this function call. */ var styleToApply = stylePerKeyword[keywordParameter]; if (styleToApply) { /*-- wrappingNode is a jQuery around whatever highlight() wraps a word in to highlight it with. Probably a <span>. */ wrappingNode.css (styleToApply); } else { console.log ( '*** Unknown keyword, "' + keywordParameter + '", in .highlight()' ); } 

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

相关问题 如何使用 react-highlight-words 突出显示具有不同 colors 的多个关键字 - how to use react-highlight-words to highlight multiple keywords with different colors 如何使用正则表达式突出显示字符串中的多个关键字/词? - How to highlight multiple keywords/words in a string with Regex? 突出显示搜索关键字结果 - Highlight search keywords result Javascript几个关键字搜索 - Javascript several keywords search 如何使用 ReactJS 在 javascript 中搜索段落中的多个关键字并用不同颜色突出显示每个关键字? - How to search multiple keywords in paragraph and highlight the each keyword with different color in javascript using ReactJS? 突出显示句子中的单独关键字 - Highlight separate keywords within a sentence 如何使用 JavaScript 突出显示包含数组中关键字的所有链接 - How to highlight all links containing keywords from array using JavaScript 如何在Sublime中突出显示“do”/“package”等对象键作为关键字? - How to not highlight object keys such as 'do'/'package' as keywords in Sublime? 如何为搜索关键字添加特定链接 - how to add specific links to search keywords 如何为几个HTML元素设置onclick函数并以关键字“ this”为目标? - how to set an onclick function to several HTML elements and aiming with the keywords “this”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM