Show Class instead of ID (jQuery)

妖精的绣舞 提交于 2019-12-13 02:14:13

问题


<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


回答1:


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

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



回答2:


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.



来源:https://stackoverflow.com/questions/5062499/show-class-instead-of-id-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!