jQuery: Open hash link relative to element id
I have some jQuery like the following:
$(".show-quiz").click(function () {
$(".quiz-container").fadeIn("fast");
});
开发者_JAVA技巧
and a link would be like:
<a href="#quiz1" class="show-quiz">Show Quiz</a>
and on the page I would have a div like:
<div id="quiz1" class="quiz-container"></div>
What I want to do is modify the jQuery so that any link with a class of show-quiz
shows the element with the id that matches the link href hash.
How do I do this?
Thanks
Try this:
$(".show-quiz").click(function (e) {
e.preventDefault();
$($(this).attr('href')).fadeIn("fast");
});
Here is a demo
精彩评论