开发者

Show Class instead of ID (jQuery)

<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


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

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


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜