显示类而不是 ID (jQuery)

发布于 2024-10-18 11:34:17 字数 283 浏览 8 评论 0原文

<a href="#tab1">Tab1</a>
<div id="tab1">content</a>
...
var a = $(this).attr('href');
$(a).show();

这有效,但仅当容器具有 ID 时,因为锚链接以“#”开头,我如何使其与类一起工作,以便它识别

content

非常感谢

<a href="#tab1">Tab1</a>
<div id="tab1">content</a>
...
var a = $(this).attr('href');
$(a).show();

This works but only if the container has an ID since the anchor link starts with "#", how do I make it work with a class, so that it recognized <div class="tab1">content</a>?

Many thanks

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

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

发布评论

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

评论(2

书间行客 2024-10-25 11:34:17
var a = $(this).attr('href');
$(a.replace('#','.')).show();

var a = $(this).attr('href').replace('#','.');
$(a).show();
var a = $(this).attr('href');
$(a.replace('#','.')).show();

var a = $(this).attr('href').replace('#','.');
$(a).show();
心在旅行 2024-10-25 11:34:17
var a = $(this).attr('href').substring(1);
$('.' + a).show();

jsFiddle

如果您是正则表达式(但是我不会在这里使用它),您可以使用...

$(this).attr('href').replace(/^#/, '.');

jsFiddle

var a = $(this).attr('href').substring(1);
$('.' + a).show();

jsFiddle.

If your a regex'r (I wouldn't use it here, however), you could use...

$(this).attr('href').replace(/^#/, '.');

jsFiddle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文