javascript - 在 javascript/jquery 中查找多个值

标签 javascript jquery html css

我有一个单词突出显示 jquery 插件,它可以检测 div 中的一个单词(在本例中为 pre),并突出显示它。我对其进行了编辑,使其可以检测多个单词,但每个函数只能检测一个单词,我希望它可以为每个函数找到多个单词(有点像语法荧光笔)。
HTML:

<!Doctype HTML>
<html>
<head>
<link rel="stylesheet" href="i.css" type="text/css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
<script src="css.js" type="text/javascript"></script>
</head>
<body onload="page();">
<button ontouchstart="page()">execute</button>
<script>
function page() {
get();
spud();
spuds();
getting();
}

function getting() {
var rego = 'get';
$('pre').highlight(rego,'green');
}

function get() {
var regi = 'hget';
    $('pre').highlight(regi,'green');
}

function spud() {
var you = 'potato';
$('pre').highlight(you,'highlight'); // change 'you' with a var value
}

function spuds() {
var your = 'chips';
   $('pre').highlight(you,'highlight'); // change 'you' with a var value
    }

</script>

<pre contenteditable="true" id="pre">
get potato chips
hget potato chips now
</pre>
</body>
</html>

CSS:

.highlight { color: red }
.green { color:green}

插件:

jQuery.fn.highlight = function(e,spill) {
function t(e, i) {
    var h = 0;
    if (3 == e.nodeType) {
        var n = e.data.toUpperCase().indexOf(i);
        if (n -= e.data.substr(0, n).toUpperCase().length - e.data.substr(0, n).length, 
        n >= 0) {
            var a = document.createElement("pig");
            a.className = spill;
            var r = e.splitText(n);
            r.splitText(i.length);
            var s = r.cloneNode(!0);
            a.appendChild(s), r.parentNode.replaceChild(a, r), h = 1;
        }
    } else if (1 == e.nodeType && e.childNodes && !/(script|style)/i.test(e.tagName)) for (var l = 0; l < e.childNodes.length; ++l) l += t(e.childNodes[l], i);
    return h;
}
return this.length && e && e.length ? this.each(function() {
    t(this, e.toUpperCase());
}) :this;
}, jQuery.fn.removeHighlight = function() {
 return this.find("pig."+spill+"").each(function() {
        with (this.parentNode.firstChild.nodeName, this.parentNode)    replaceChild(this.firstChild, this), 
      normalize();
 }).end();
};

我的目标是在变量 rego regi you your 中有多个值 .例如,在 var your 中,我想要这样的内容:var your = "chips","kumara","grease"; ,我已经尝试过 var your = ["chips","kumara","grease"];,但它不起作用。

最佳答案

可以尝试替换:

return this.length && e && e.length ? this.each(function() {
    t(this, e.toUpperCase());
}) :this;

return this.length && e && e.length ? this.each(function() {
    var arr = typeof e === 'string' ? [e] : e,
        self=this; 
    $.each(arr, function(_, term){
        if(term.length){
           t(self, term.toUpperCase());
        }
    });    
}) :this;

它现在正在检查该值是字符串还是数组,因此您可以使用其中之一

关于javascript - 在 javascript/jquery 中查找多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30145055/

相关文章:

javascript - 单击链接提交过滤器表单

javascript - 如何知道 WebRTC 连接使用什么加密?

javascript - jQuery/javascript 在列表中查找值

javascript - PHP 无法正确接收我表单中的数据

javascript - 如何在 phonegap javascript 函数中返回结果?

jquery - 使用正则表达式和 jQuery 选择阿拉伯文本中的模式

javascript - 使用jquery和css隐藏菜单栏div下方的容器div

jquery - 将 CSV 转换为 JSON - jQuery

html - 使 PDF 帮助文件自动打开

html - 将媒体查询从最小宽度更改为使用最大宽度以避免覆盖?