简体   繁体   中英

jQuery attribute selector using *=

Look at the following jQuery selector: $('img[alt*="Robin"]') . What does it select?

I understand this line of code...EXCEPT the * part. What does this * mean?

It means the alt attribute needs to contain Robin (and not necessarily equal it). For example,

<img alt="whoisRobin"/>
<img alt="Robin"/>
<img alt="Robinaa"/>

would be selected, but the following would not:

<img alt="Robi"/>

More info in jQuery docs

* represents a value containing a specific string.

Any image with alt attribute contains "Robin"

It's the attribute contains selector.

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

Selects elements that have the specified attribute with a value containing the a given substring

It will

Finds all inputs with a alt attribute that contains Robin and sets the value with some text.

From attribute-contains-selector

Selects elements that have the specified attribute with a value containing the a given substring

For egs ,

<img alt="Robin-news" src='...'/>
<img alt="xyzRobin" src='...'/>
<img alt="letterRobin2" src='...'/>
<img alt="newone" src='...'/>

Script

$('img[alt*="Robin"]');// outputs First 3 image objects

The * matches a substring in the attribute's value. So, images with alt attribute values containing the substring "Robin" are being selected.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM