开发者

jQuery attribute selector - am I doing something wrong?

开发者 https://www.devze.com 2023-04-11 17:25 出处:网络
var selector = \"ul.lower-menu li a[innerText=\\\"\" + PanelSettings[tab_name_key] + \"\\\"]\"; I\'m trying to get horizontal menu tab that has innerText property set to the previously stored value.
var selector = "ul.lower-menu li a[innerText=\"" + PanelSettings[tab_name_key] + "\"]";

I'm trying to get horizontal menu tab that has innerText property set to the previously stored value.

The config value is stored like this:

PanelSettings[tab_name_key] = $("ul.lower-menu li:first > a").prop("innerText");

It is actually there and it always has the proper value.

I get nothing from jQuery - am I doing something obviously wrong here?

Edit: I'开发者_如何学Cm using jQuery 1.6.4


Yes, you are doing something wrong. The attribute selector selects elements that have a certain attribute set. innerText is a property, not an attribute – as indeed you recognise by using prop to set it.

You have two options. One is to use the :contains selector:

"ul.lower-menu li a:contains(" + PanelSettings[tab_name_key] + ")"

This, however, would select foobar if you asked for foo. The better option is to do the filtering yourself, using filter:

var selector = "ul.lower-menu li a";
var els = $(selector).filter(function() {
    return $.text([this]) === PanelSettings[tab_name_key];
});

This selects only the elements whose text content is the same as the setting given.

NB also, as Diodeus says, you should use text() rather than prop('innerText').


You might want to try filter instead of the attribute selector, which IMHO borders on the abusive in this case:

var selector = "ul.lower-menu li a";
var $links = $(selector).filter(function() {
                 return $(this).text() == PanelSettings[tab_name_key];
             });


Instead of:

.prop("innerText");

use:

 .text()


Maybe you want to use the :contains(text) jQuery selector?

http://api.jquery.com/contains-selector/


well, it is an attribute selector, not a property selector. you can't use selectors like that. use .filter() with the function() variant instead

0

精彩评论

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