开发者

Can JQuery select by a <label>'s text or enumerate its attributes?

开发者 https://www.devze.com 2023-02-06 17:46 出处:网络
I\'m attempting: $(\'label[text=\"someValue\"]) but am getting an empty set returned, most probably since text isn\'t an attribute.

I'm attempting:

$('label[text="someValue"])

but am getting an empty set returned, most probably since text isn't an attribute.

Is it possible to select by the element's text or inner html?

Edit: :contains("someValue) is not strict enough, since it will return any matches of someValue as a substring.

Is there a way to enumerate all the开发者_Go百科 element's attributes to investigate/interrogate them during debugging/execution?


You can select them all and then filter down to a smaller subset by using filter:

$('label').filter(function () {
    return $(this).text() === "someValue";
});


Tested this:

    $.each($("#form label"), function() {
        var nodes = this.attributes;
        for(var i=0; i<nodes.length; i++)
        {
          alert(nodes[i].nodeName);
         alert(nodes[i].nodeValue);
        }
    });


There's a function for getting an element's text:

http://api.jquery.com/html/

var labelText = $('.parentClass label').html();

What attributes would you add to a label? You could assign variables to attributes of the label (i'd usually want the for attribute if I was working with labels):

var labelAttr = $('.parentClass label').attr('for');
0

精彩评论

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

关注公众号