如何使用 jQuery 更改选项卡式内容?

发布于 2024-09-08 05:56:57 字数 4342 浏览 4 评论 0原文

我正在使用 jQuery 在三个单独的视图中显示内容。但是,我只能显示第一个(默认)视图。注意:这没有使用 jQuery UI。

这是页面的链接(仍在开发中)。

这是 html:

<div id="tabbed_area">
            <ul class="tabs">
                <li><a id="services" class="tab active" href="#">Services</a></li>
                <li><a id="supplies" class="tab" href="#">Supplies</a></li>
                <li><a id="diy" class="tab" href="#">DIY</a></li>
            </ul>
            <br class="clearer" />
                <div id="services-content" class="content">
                    <p class="intro">Welcome to <a href="#">Moving Simplified</a>. Features and benefits go here on the home page to drive.</p>
                    <h3>This is the main headline</h3>
                    <h4>Headline Goes Here</h4>
                    <p>Welcome to Moving Simplified. Features and benefits go here on the ome page to drive home the point that you are affordable, professional, knowledgeable, amd make the move real.</p>
                </div>
                <div id="supplies-content" class="content">
                    <p class="intro">Welcome to <a href="#">Moving Simplified</a>. Features and benefits go here on the home page to drive.</p>
                    <h3>This is the main headline</h3>
                    <h4>Headline Goes Here</h4>
                    <p>Welcome to Moving Simplified. Features and benefits go here on the ome page to drive home the point that you are affordable, professional, knowledgeable, amd make the move real.</p>
                </div>
                <div id="diy-content" class="content">
                    <p class="intro">Welcome to <a href="#">Moving Simplified</a>. Features and benefits go here on the home page to drive.</p>
                    <h3>This is the main headline</h3>
                    <h4>Headline Goes Here</h4>
                    <p>Welcome to Moving Simplified. Features and benefits go here on the ome page to drive home the point that you are affordable, professional, knowledgeable, amd make the move real.</p>
                </div>
        </div>

这是 css:

#mainContent #tabbed_area           { padding: 0 20px; position: relative; z-index: 50; }
ul.tabs                 { margin: 0px; padding: 0px; }
ul.tabs li                  { list-style: none; float: left; display: inline; }
ul.tabs li a#services           { 
                        width: 113px;
                        height: 36px;
                        display: block;
                        text-indent: -9999px;
                        border: none;
                        background: url(../_images/btn_services.png) no-repeat center top; }
ul.tabs li a#supplies               { 
                        width: 113px;
                        height: 36px;
                        display: block;
                        text-indent: -9999px;
                        border: none;
                        background: url(../_images/btn_supplies.png) no-repeat center bottom; }                                 
ul.tabs li a#diy                    { 
                        width: 113px;
                        height: 36px;
                        display: block;
                        text-indent: -9999px;
                        border: none;
                        background: url(../_images/btn_diy.png) no-repeat center bottom; }

 ul.tabs li a#services:hover,
 ul.tabs li a#supplies:hover,
 ul.tabs li a#diy:hover  { background-position: center center; }                                

#supplies-content, #diy-content { display: none; }

最后是 jQuery:

<script type="text/javascript">
$(document).ready(function() {
$("a.tab").click(function(){
    $(".active").removeClass("active");
    $(this).addClass("active");
    $(.content).slideUp();

    var content_show = $(this).attr("title");
    $("#"+content_show).slideDown();
    });
});
</script> 

I am using jQuery to display content in three separate views. However, I can only get the first (default) view to show. Note: this is not using jQuery UI.

Here is a link to the page (still very much in dev).

Here is the html:

<div id="tabbed_area">
            <ul class="tabs">
                <li><a id="services" class="tab active" href="#">Services</a></li>
                <li><a id="supplies" class="tab" href="#">Supplies</a></li>
                <li><a id="diy" class="tab" href="#">DIY</a></li>
            </ul>
            <br class="clearer" />
                <div id="services-content" class="content">
                    <p class="intro">Welcome to <a href="#">Moving Simplified</a>. Features and benefits go here on the home page to drive.</p>
                    <h3>This is the main headline</h3>
                    <h4>Headline Goes Here</h4>
                    <p>Welcome to Moving Simplified. Features and benefits go here on the ome page to drive home the point that you are affordable, professional, knowledgeable, amd make the move real.</p>
                </div>
                <div id="supplies-content" class="content">
                    <p class="intro">Welcome to <a href="#">Moving Simplified</a>. Features and benefits go here on the home page to drive.</p>
                    <h3>This is the main headline</h3>
                    <h4>Headline Goes Here</h4>
                    <p>Welcome to Moving Simplified. Features and benefits go here on the ome page to drive home the point that you are affordable, professional, knowledgeable, amd make the move real.</p>
                </div>
                <div id="diy-content" class="content">
                    <p class="intro">Welcome to <a href="#">Moving Simplified</a>. Features and benefits go here on the home page to drive.</p>
                    <h3>This is the main headline</h3>
                    <h4>Headline Goes Here</h4>
                    <p>Welcome to Moving Simplified. Features and benefits go here on the ome page to drive home the point that you are affordable, professional, knowledgeable, amd make the move real.</p>
                </div>
        </div>

Here is the css:

#mainContent #tabbed_area           { padding: 0 20px; position: relative; z-index: 50; }
ul.tabs                 { margin: 0px; padding: 0px; }
ul.tabs li                  { list-style: none; float: left; display: inline; }
ul.tabs li a#services           { 
                        width: 113px;
                        height: 36px;
                        display: block;
                        text-indent: -9999px;
                        border: none;
                        background: url(../_images/btn_services.png) no-repeat center top; }
ul.tabs li a#supplies               { 
                        width: 113px;
                        height: 36px;
                        display: block;
                        text-indent: -9999px;
                        border: none;
                        background: url(../_images/btn_supplies.png) no-repeat center bottom; }                                 
ul.tabs li a#diy                    { 
                        width: 113px;
                        height: 36px;
                        display: block;
                        text-indent: -9999px;
                        border: none;
                        background: url(../_images/btn_diy.png) no-repeat center bottom; }

 ul.tabs li a#services:hover,
 ul.tabs li a#supplies:hover,
 ul.tabs li a#diy:hover  { background-position: center center; }                                

#supplies-content, #diy-content { display: none; }

And finally here is the jQuery:

<script type="text/javascript">
$(document).ready(function() {
$("a.tab").click(function(){
    $(".active").removeClass("active");
    $(this).addClass("active");
    $(.content).slideUp();

    var content_show = $(this).attr("title");
    $("#"+content_show).slideDown();
    });
});
</script> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白鸥掠海 2024-09-15 05:56:57

这一行:

    $(.content).slideUp();

应该是:

    $(".content").slideUp();

而这一行:

var content_show = $(this).attr("title");

应该是:

var content_show = $(this).attr("id");

...因为你没有标题属性,但 ID 似乎确实给了你想要的。

由于内容区域的 ID 以“-content”结尾,因此您需要将其附加到 content_show 中的 ID。

$("#" + content_show + '-content').slideDown();

另外,我猜测 元素的默认行为是在您单击时刷新页面。

您应该使用 event.preventDefault() 单击时阻止 的默认行为

$("a.tab").click(function( evt ) {
    // Prevents the default behavior of the <a> from occurring
    evt.preventDefault();
    $(".active").removeClass("active");
    $(this).addClass("active");
    var content_show = $(this).attr("id");
         // only slide up the visible content
    $(".content:visible").slideUp();
         // you need to append '-content' to the ID to make
         //    match the proper content area
    $("#" + content_show + '-content').slideDown();

});​

This line:

    $(.content).slideUp();

should be:

    $(".content").slideUp();

And this:

var content_show = $(this).attr("title");

should be:

var content_show = $(this).attr("id");

...since your doesn't have a title attribute, but the ID does seem to give you want you want.

And since the content area has an ID ending with "-content", you need to append that to the ID from content_show.

$("#" + content_show + '-content').slideDown();

Also, I'm guessing that the default behavior of the <a> element is refreshing the page when you click.

You should prevent the default behavior of the <a> when you click using event.preventDefault()

$("a.tab").click(function( evt ) {
    // Prevents the default behavior of the <a> from occurring
    evt.preventDefault();
    $(".active").removeClass("active");
    $(this).addClass("active");
    var content_show = $(this).attr("id");
         // only slide up the visible content
    $(".content:visible").slideUp();
         // you need to append '-content' to the ID to make
         //    match the proper content area
    $("#" + content_show + '-content').slideDown();

});​
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文