【问题标题】:Finding multiple values in javascript/jquery在 javascript/jquery 中查找多个值
【发布时间】:2015-05-10 05:33:28
【问题描述】:

我有一个单词突出显示 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"];,但它不起作用。

【问题讨论】:

  • 你在说什么......你写了这个插件并想要添加这个功能的方向?
  • 不,我下载了插件,我想让它检测多个单词
  • 除了forEach还有别的方法吗?我不太清楚如何把它放进去。

标签: javascript jquery html css


【解决方案1】:

可以尝试更换:

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;

它现在正在检查该值是字符串还是数组,以便您可以使用任何一种

【讨论】:

    猜你喜欢
    • 2015-10-05
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多