Can JQuery select by a <label>'s text or enumerate its attributes?
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');
精彩评论