当我点击一个链接时,我把它的href值放在一个变量中(这个数字代表一个唯一的id),然后...我有一个单独的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>
我尝试做的是看看我的"ul“中是否有任何"a”,它的id与我在变量中存储的id相同。
我尝试了这样的方法(当然没有结果):
$('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 + '"]')
发布于 2009-10-23 06:02:54
你试过交换你的报价吗?jQuery可能正在使用单引号进行属性选择匹配...
$('ul.class_one').find("a[href='" + myVar + "']");
来自jQuery Documentation
属性值。引号在大多数情况下是可选的,但当值包含"]“等字符时,应使用引号以避免冲突。可以通过以下语法使用变量: name='“+MyVar+ "'
发布于 2009-10-23 06:04:59
完全删除引号是可能的。
$('ul.class_one').find('a[href='+myVar+']');
应该行得通
发布于 2009-10-23 06:02:23
CSS和扩展名为jQuery都使用单引号。
$("ul.class_one").find("a[href='" + myVar + "']");
https://stackoverflow.com/questions/1610220
复制相似问题