jquery遍历

发布于 2024-08-08 04:44:12 字数 570 浏览 1 评论 0原文

当我点击一个链接时,我在变量中获取它的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>

我想做的是看看我是否有任何“a”在我的“ul”中,它的 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 + '"]')

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 + '"]')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌能量女王 2024-08-15 04:44:12

您是否尝试过交换报价? jQuery 可能使用单引号进行属性选择匹配...

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

来自 jQuery 文档

属性值。报价是
大多数情况下是可选的,但应该是
用于避免值冲突
包含“]”等字符。
可以使用变量
以下语法: [name='" +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+ "']

伴我心暖 2024-08-15 04:44:12

可以完全删除引号...

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

应该可以

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

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

Should work

与往事干杯 2024-08-15 04:44:12

CSS 和 jQuery 都使用单引号。

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

CSS, and jQuery by extension, uses single quotes.

$("ul.class_one").find("a[href='" + myVar + "']");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文