Show Class instead of ID (jQuery)

2019-08-01 10:15发布

<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

2条回答
Fickle 薄情
2楼-- · 2019-08-01 11:03
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.

查看更多
小情绪 Triste *
3楼-- · 2019-08-01 11:14
var a = $(this).attr('href');
$(a.replace('#','.')).show();

var a = $(this).attr('href').replace('#','.');
$(a).show();
查看更多
登录 后发表回答