开发者

JQuery: Make contains case insensitive

I have looked at other solutions to this on this site, but can't see how to adapt for my situation. I have the following code which looks for a label containing 'Size', but it needs to find 'size' too.

    <script type="text/javascript">
        $(document).ready(function() {                                                       

            i开发者_StackOverflow社区f ( $('.productOptionsBlock label:contains("Size")').length > 0 ) {
                $('.productOptionsBlock label:contains("Size")').replaceWith('<label>Please select your size:</label>');
            }

        });
    </script>       

As a JQuery newby, I can't see how to apply the other functions I've found...

Many thanks,

Andy


Here we are defining our own expression instead of changing system's one with name "contains-ci" (contains which is case-insensitive):

$.extend($.expr[":"],
{
    "contains-ci": function(elem, i, match, array)
    {
        return (elem.textContent || elem.innerText || $(elem).text() || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
    }
});

Post on my blog where you can find some example which works on top of this expression:

http://blogs.digitss.com/javascript/jquery-javascript/jquery-case-insensitive-contains-expression/

http://blogs.digitss.com/javascript/jquery-javascript/implementing-quick-table-search-using-jquery-filter/


$.expr[':'].Contains = function(x, y, z){
    return jQuery(x).text().toUpperCase().indexOf(z[3].toUpperCase())>=0;
};


$('.productOptionsBlock label:contains("Size") , .productOptionsBlock label:contains("size")').replaceWith('<label>Please select your size:</label>');
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜