简体   繁体   English

找到一个特定的单词并用span标签包装

[英]find a specific word and wrap with a span tag

i am not sure the best way to go about this. 我不确定最好的办法。 here is the problem. 这是问题所在。 i have a menu (list items) that has an irregular pattern of display. 我有一个菜单(列表项)具有不规则的显示模式。 i cant alter the html directly as it is being spit out of a cms. 我不能直接改变html,因为它是从cms吐出来的。 i would like it so that if a word in an array matches a word in the link then that word has a span wrapped around it. 我希望如此,如果一个数组中的单词与链接中的单词匹配,那么该单词就会有一个跨度。 this is that code simplified: 这是简化的代码:

<li class="item1"><a href="gosomewhere.html">About My Store</a></li>
<li class="item2"><a href="gosomewhereelse.html">Find Locations</a></li>

result i want is this: 我想要的结果是这样的:

<li class="item1"><a href="gosomewhere.html"><span class="bigwords">About My </span>Store</a></li>
<li class="item2"><a href="gosomewhere.html">Find <span class="bigwords">Locations</span></a></li>

i can find the first word(s) doing this: 我可以找到这样做的第一个单词:

      $(function() {
    $.fn.wrapStart = function (numWords) {
    var node = this.contents().filter(function () { return this.nodeType == 3 }).first(),
        text = node.text(),
        first = text.split(" ", numWords).join(" ");

    if (!node.length)
        return;

    node[0].nodeValue = text.slice(first.length);
    node.before('<span class="bigwords">' + first + '</span>');
};


$("li.item1 a").wrapStart(1);

however, what i would like to do is look through an array list and wrap those specific words that i want in whatever position they occur. 但是,我想要做的是查看一个数组列表,并将我想要的那些特定单词包装在它们出现的任何位置。

the array would be like about, store, faq... 数组就像是关于,存储,常见问题......

the script would look for those words and wrap them... 脚本会查找这些单词并将它们包装起来......

not really sure how to go about this. 不太确定如何解决这个问题。 if its easier. 如果它更容易。 since the big words only occur either at the beginning or the end but not in middle... i could have another function that looks for the last word(s). 因为大词只出现在开头或结尾但不出现在中间...我可以有另一个函数来寻找最后一个单词。

there are only 5 menu items (wont change) and the words will never change, which is why i thought of just using an array... 只有5个菜单项(不会改变),单词永远不会改变,这就是为什么我想只使用一个数组...

here is the output i get of the plugin use: 这是插件使用的输出:

<ul class="menu><li class="item-102 parent">
<a class="about" title="About my store" href="#">
<span class="bigwords">
<a xmlns="http://www.w3.org/1999/xhtml">About</a>
</span>
<a xmlns="http://www.w3.org/1999/xhtml"> my store</a>
</a>
</li></ul>

I wrote a simple plugin recently that could accomplish this: 我最近写了一个简单的插件,可以完成这个:

(it no longer exists) (它不再存在)

you would use it like this: 你会像这样使用它:

var arr = ["about my","locations"];
$.each(arr,function(i,word){
  $(".items").highlightText(word,"bigwords",true)
});

http://jsfiddle.net/4gtmH/23/ http://jsfiddle.net/4gtmH/23/

Since i'm using a regexp, you could also do it this way: 因为我正在使用正则表达式,所以你也可以这样做:

$(".items").highlightText("about my|locations","bigwords",true)

Also: 也:

I used .items as a class because in my example i gave the ul a class of items. 我使用.items作为一个类,因为在我的例子中我给了ul一类项目。 you can supply any selector and it will highlight all text within the element and it's children. 您可以提供任何选择器,它将突出显示元素中的所有文本及其子项。

Here's the part of the plugin that does the work, just in case that link goes dead for some reason: 以下是插件执行工作的部分,以防链接因某种原因而死亡:

$(this).find("*").andSelf().contents()

// filter to only text nodes that aren't already highlighted
.filter(function () {
    return this.nodeType === 3 && $(this).closest("." + hClass).length === 0;
})

// loop through each text node
.each(function () {
    var output;
    output = this.nodeValue.replace(re, "<" + defaultTagName + " class='" + hClass + "'>$1</" + defaultTagName + ">");
    if (output !== this.nodeValue) {
        $(this).wrap("<p></p>").parent()
            .html(output).contents().unwrap();
    }
});

Have you looked at this jQuery plugin ? 你看过这个jQuery插件了吗? Haven't used it myself but looks like it does the job pretty easily. 没有自己使用它,但看起来它很容易完成工作。

您可以在所有元素上创建一个jquery选择器,并且foreach结果在选择器中设置val,并将val替换为数据

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM