最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Tilde in jQuery selector - Stack Overflow

programmeradmin3浏览0评论

My understanding of the tilde's function in Javascript is that it performs a bitwise not operation (i.e. 1 becomes 0 and vice versa; 1000 becomes 0111). However, I've recently begun work on an existing project where my predecessor has included a lot of code like this:

var iValuation = $('div[class~="iValuation"]');

Can anyone tell me what the purpose of the tilde in this instance is? I've not come across it before and haven't been able to find any reference to it online.

My understanding of the tilde's function in Javascript is that it performs a bitwise not operation (i.e. 1 becomes 0 and vice versa; 1000 becomes 0111). However, I've recently begun work on an existing project where my predecessor has included a lot of code like this:

var iValuation = $('div[class~="iValuation"]');

Can anyone tell me what the purpose of the tilde in this instance is? I've not come across it before and haven't been able to find any reference to it online.

Share Improve this question edited Oct 24, 2019 at 18:59 Nelu 18.7k10 gold badges87 silver badges93 bronze badges asked Feb 18, 2013 at 16:21 Dorian FabreDorian Fabre 5371 gold badge9 silver badges25 bronze badges 2
  • 1 Browsing api.jquery.com/category/selectors leads one to this – Blazemonger Commented Feb 18, 2013 at 16:23
  • That's not a JavaScript operator, it's a jQuery selector. The API has an entire page about them. – Anthony Grist Commented Feb 18, 2013 at 16:23
Add a comment  | 

4 Answers 4

Reset to default 11

Tiled used as selector means

Selects elements that have the specified attribute with a value containing a given word, delimited by spaces.

which is not a JavaScript operator at all.

More from doc:

This selector matches the test string against each word in the attribute value, where a "word" is defined as a string delimited by whitespace. The selector matches if the test string is exactly equal to any of the words.

For example:

<input name="man-news" />
<input name="milk man" />
<input name="letterman2" />
<input name="newmilk" />

$('input[name~="man"]') will select only second input, because its attribute name is separated by space.

For detail see here

That isn't a JavaScript operator. It appears in a string.

Since that string is passed to the jQuery function, and it doesn't look like a piece of HTML, it is a selector.

Specifically one of the attribute selectors:

Represents an element with the att attribute whose value is a whitespace-separated list of words, one of which is exactly "val". If "val" contains whitespace, it will never represent anything (since the words are separated by spaces). Also if "val" is the empty string, it will never represent anything.

$ is the jQuery selector function, which contains a CSS3 Selector String: According to the CSS3 Selector Definition, the selector you encountered selects:

E[foo~="bar"] an E element whose "foo" attribute value is a list of whitespace-separated values, one of which is exactly equal to "bar"

in the DOM. Because the Tilde is wrapped in a string, it is not working as an operator.

In case you're wondering about the difference between

[class~="foo"]

and

[class*="foo"]

~ will match only with whitespace around (e.g. 'foo bar' but not 'foo-1')
* will match with or without whitespace around (e.g. 'foo bar' and 'foo-1')

~ - Attribute Spaced Selector
* - Attribute Contains Selector

发布评论

评论列表(0)

  1. 暂无评论