开发者

jquery traversing

开发者 https://www.devze.com 2022-12-09 09:58 出处:网络
when i click on 开发者_开发技巧a link i take it\'s href value in a variable (the number represents an unique id), then...i have a separate ul:

when i click on 开发者_开发技巧a link i take it's href value in a variable (the number represents an unique id), then...i have a separate ul:

<ul class="class_one">
   <li class="class_two"><a href="2345">some text</a></li>
   <li class="class_three"><a href="6789">some text</a></li>
</ul>

what i'm trying to do is to see if i have any "a" in my "ul" that has the same id that i stocked in my variable.

i tried something like this( of course with no result ):

$('ul.class_one').find('a[href="' + myVar + '"]')
//and if it finds no element do something
//i think is something wrong with the quotes ('a[href="' + myVar + '"]')


Have you tried swapping your quotes? jQuery might be using single quotes for attribute selection matching...

$('ul.class_one').find("a[href='" + myVar + "']");

From jQuery Documentation

An attribute value. Quotes are optional in most cases, but should be used to avoid conflicts when the value contains characters like "]". Variables can be used using the following syntax: [name='" +MyVar+ "']


It's possible to remove the quotes altogether...

$('ul.class_one').find('a[href='+myVar+']');

Should work


CSS, and jQuery by extension, uses single quotes.

$("ul.class_one").find("a[href='" + myVar + "']");
0

精彩评论

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