开发者

JQuery select by inner text

开发者 https://www.devze.com 2023-04-05 23:31 出处:网络
I am doing a project with fullcalendar and I need to be able to select an element based on the day number.Here is a sample of the element I need to select:

I am doing a project with fullcalendar and I need to be able to select an element based on the day number. Here is a sample of the element I need to select:

<div class=​"fc-day-number">​1​</div>​

Here is the javascript I have written:

$("div.fc-day-number:contains(" + day + ")").parent().parent().css({ 'background-color': 'Green' });

This will grab all the divs whose inner text contains the day variable, problem is if day==1 then it also selects 11,12,13,21,31,etc. How do I write a JQuery selector that will grab the divs with cl开发者_如何学JAVAass "fc-day-number" and whose inner text is exactly equal to day?


Write your own filter function:

$("div.fc-day-number").filter(function (){
    return $(this).text() == day;
}).parent().parent().css({ 'background-color': 'Green' });


$("div.fc-day-number").each(function (){
    if($(this).text()==day)
    {
        $(this).css({ 'background-color': 'Green' });
    }
});


jQuery.expr[":"].text = jQuery.expr.createPseudo(function(arg)
{
    return function( elem ) {
        return jQuery(elem).text().toLowerCase() == arg;
    };
});

This code will add the new pseudoselector which will filter elements the internal text which isn't equal to arg. For you example use here so:

$("div.fc-day-number:text(" + day + ")").parent().parent().css({ 'background-color': 'Green' });
0

精彩评论

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