Pass an array into jquery attr(), for one property
Can I Pass an array into jquery attr for one property, such as follow
$("th[id={x,y}]")
it look like "where in" condination at SQL. My wants su开发者_如何学编程ch as it, $("th[id={x,y}]") is th's id equal one of these? I want to ask it! Do you use any where-in condition in sql.. My wants like it.
EDIT: you asked:
My wants such as it, $("th[id={x,y}]") is th's id equal one of these? I want to ask it! Do you use any where-in condition in sql.. My wants like it
I assume you are looking for a way to select multiple <th>
elements based on one or more ids. You can simply chain selectors like this:
$('th#x, th#y, th#z'); // will return 3 elements, if they exist
Original answer:
No.
jQuery.attr()
is used to set valid properties on HTML objects.
If you want to store arbitrary data linked to a HTML object, use jQuery.data()
.
[EDIT] the question has been modified to clarify what he wants, so I've removed the inapplicable parts of my answer...
JQuery (and CSS) allows you to use multiple selectors together:
For example:
$('[id=x], [id=y], [id=z]')
...will select all elements in your page with either of your possible id
values.
Alternatively, if you only want to find them within $(this)
, you would use the find()
method:
$(this).find('[id=x], [id=y], [id=z]')
Hope that answers you're question.
Depending on the values you're searching for, you may also be interested in more advanced attribute selectors. For example, if your x
, y
and z
options all begin with the same string, you could use the attribute prefix selector:
$('[id|=prefix]')
or if they all contain a particular word:
$('[id~=word]')
there are a number of possible options; that should give you a start, but see the JQuery manual for more information: http://api.jquery.com/category/selectors/
Would this work for your needs - replace { with "?
$(this).attr("alt","1,2,3")
精彩评论