开发者

jQuery: Open hash link relative to element id

开发者 https://www.devze.com 2023-03-25 00:12 出处:网络
I have some jQuery like the following: $(\".show-quiz\").click(function () { $(\".quiz-container\").fadeIn(\"fast\");

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

0

精彩评论

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