开发者

Where to place jQuery.expr code?

I'm looking for a solution to have a case-insensitive version of the :contains selector in jQuery. I found these two posts here on Stack Overflow:

Is there a case insensitive jQuery :contains selector?

How do I make jQuery Contains case insensitive, including jQuery 1.8+?

So basically the solution is this:

jQuery.expr[':'].Contains = function(a,i,m){
    return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
};

My only question is how do I use this? Where to I specify this piece of code? I'm loading jQuery in my header and afterwards I load my script.js file where I have my DOM ready function declared. I put this piece of code outside the DOM ready function but it doesn't cha开发者_开发知识库nge anything in the behaviour of the :contains selector.


If you put this piece of code after loading jquery, you will have one contains and one (case-insensitive) icontains selector:

$.extend($.expr[':'], {
        // case insensitive version of :contains
        icontains: function(a,i,m){
            return (a.textContent||a.innerText||jQuery(a).text()||"").
                toLowerCase().indexOf(m[3].toLowerCase())>=0;
        }
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜