单击时,jQuery选项卡不会单击

时间:2013-10-09 20:44:36

标签: javascript jquery

我有使用ui 1.10.0的jQuery选项卡。我的第一个标签默认激活;但是,我无法让其他5个标签工作。当我点击它们时,它们什么都不做,并且与标签协调的内容不显示。

以下是基本的HTML代码:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tabs</title>
<script src="js/jquery-ui-1.10.0.custom.min.js" script type="text/javascript"></script>
<script src="js/jquery-1.9.0.min.js" script type="text/javascript"></script>
<link rel="stylesheet" href="css/jquery-ui-1.10.0.custom.min.css" type="text/css"/>
<link rel="stylesheet" href="css/style.css" type="text/css"/>
</head>
<body>
<div id="simpaleTabs" class="ui-tabs ui-widget ui-widget-content ui-corner-all" >
    <ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all">
        <li class="ui-state-default ui-corner-top ui-tabs-active ui-state-active">
            <a href="#production" class="ui-tabs-anchor">Production</a>
         </li>
        <li class="ui-state-default ui-corner-top">
            <a href="#invoices" class="ui-tabs-anchor">Invoices</a>
         </li>
        <li class="ui-state-default ui-corner-top">
            <a href="#warranty" class="ui-tabs-anchor">Warranty</a>
         </li>
        <li class="ui-state-default ui-corner-top">
            <a href="#services" class="ui-tabs-anchor">Services</a>
         </li>
        <li class="ui-state-default ui-corner-top">
            <a href="#parts" class="ui-tabs-anchor">Parts List</a>
          </li>
        <li class="ui-state-default ui-corner-top"><a href="#memorandums" class="ui-tabs-anchor">Memorandums</a>
        </li>
    </ul>
    <div id="facebook" class="ui-tabs-panel ui-widget-content ui-corner-bottom">
                content about Proudction here 
    </div>
    <div id="twitter" class="ui-tabs-panel ui-widget-content ui-corner-bottom">
                content about Invoices here
    </div>
    <div id="linkedin" class="ui-tabs-panel ui-widget-content ui-corner-bottom">
                content about Warranty here
    </div>
    <div id="google" class="ui-tabs-panel ui-widget-content ui-corner-bottom">
                content about Services here
    </div>
    <div id="wikipedia" class="ui-tabs-panel ui-widget-content ui-corner-bottom">
                content about Parts List here
    </div>
    <div id="picasa" class="ui-tabs-panel ui-widget-content ui-corner-bottom">
                content about Memorandums here
    </div>
</div>

这是Javascript:

<script>
$(document).ready(function(){
    $('#simpaleTabs div').hide();
    $('#simpaleTabs div:first').show();
    $('#simpaleTabs ul li:first').addclass('active');

    $('#simpaleTabs ul li a').click(function(){
        $('#simpaleTabs ul li').removeClass('active');
        $(this).parent().addClass('active');
        var currentTab = $(this).attr('href');
        $('#simpaleTabs div').hide();
        $(currentTab).show();
        return false;
    });
    });
</script>

2 个答案:

答案 0 :(得分:1)

下一行的addclass中有一个拼写错误。您已指定addclass而不是addClass。

$('#simpaleTabs ul li:first').addclass('active');

以下是更新的js。

$(document).ready(function () {
    $('#simpaleTabs div').hide();
    $('#simpaleTabs div:first').show();
    $('#simpaleTabs ul li:first').addClass('active');

    $('#simpaleTabs ul li a').click(function () {
        $("li").each(function () {
            $(this).removeClass('ui-tabs-active');
            $(this).removeClass('ui-state-active');
        });
        $(this).parent().addClass('ui-tabs-active');
        $(this).parent().addClass('ui-state-active');
        var currentTab = $(this).attr('href');
        $('#simpaleTabs div').hide();
        $(currentTab).show();
        return false;
    });
});

这是Demo Fiddle

如果您感兴趣的话,这里有更正的HTML

<div id="simpaleTabs" class="ui-tabs ui-widget ui-widget-content ui-corner-all">
    <ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all">
        <li class="ui-state-default ui-corner-top ui-tabs-active ui-state-active"> <a href="#production" class="ui-tabs-anchor">Production</a>

        </li>
        <li class="ui-state-default ui-corner-top"> <a href="#invoices" class="ui-tabs-anchor">Invoices</a>

        </li>
        <li class="ui-state-default ui-corner-top"> <a href="#warranty" class="ui-tabs-anchor">Warranty</a>

        </li>
        <li class="ui-state-default ui-corner-top"> <a href="#services" class="ui-tabs-anchor">Services</a>

        </li>
        <li class="ui-state-default ui-corner-top"> <a href="#parts" class="ui-tabs-anchor">Parts List</a>

        </li>
        <li class="ui-state-default ui-corner-top"><a href="#memorandums" class="ui-tabs-anchor">Memorandums</a>

        </li>
    </ul>
    <div id="production" class="ui-tabs-panel ui-widget-content ui-corner-bottom">content about Proudction here</div>
    <div id="invoices" class="ui-tabs-panel ui-widget-content ui-corner-bottom">content about Invoices here</div>
    <div id="warranty" class="ui-tabs-panel ui-widget-content ui-corner-bottom">content about Warranty here</div>
    <div id="services" class="ui-tabs-panel ui-widget-content ui-corner-bottom">content about Services here</div>
    <div id="parts" class="ui-tabs-panel ui-widget-content ui-corner-bottom">content about Parts List here</div>
    <div id="memorandums" class="ui-tabs-panel ui-widget-content ui-corner-bottom">content about Memorandums here</div>
</div>

答案 1 :(得分:0)

在HTML和JS中有一些错误。

试试这个

HTML:你必须在div

的href属性中放入div的正确'id'
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tabs</title>
<script src="js/jquery-ui-1.10.0.custom.min.js" script type="text/javascript"></script>
<script src="js/jquery-1.9.0.min.js" script type="text/javascript"></script>
<link rel="stylesheet" href="css/jquery-ui-1.10.0.custom.min.css" type="text/css"/>
<link rel="stylesheet" href="css/style.css" type="text/css"/>
</head>
<body>
<div id="simpaleTabs" class="ui-tabs ui-widget ui-widget-content ui-corner-all" >
    <ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all">
        <li class="ui-state-default ui-corner-top ui-tabs-active ui-state-active">
            <a href="#production" class="ui-tabs-anchor">Production</a>
         </li>
        <li class="ui-state-default ui-corner-top">
            <a href="#invoices" class="ui-tabs-anchor">Invoices</a>
         </li>
        <li class="ui-state-default ui-corner-top">
            <a href="#warranty" class="ui-tabs-anchor">Warranty</a>
         </li>
        <li class="ui-state-default ui-corner-top">
            <a href="#services" class="ui-tabs-anchor">Services</a>
         </li>
        <li class="ui-state-default ui-corner-top">
            <a href="#parts" class="ui-tabs-anchor">Parts List</a>
          </li>
        <li class="ui-state-default ui-corner-top"><a href="#memorandums" class="ui-tabs-anchor">Memorandums</a>
        </li>
    </ul>
    <div id="production"" class="ui-tabs-panel ui-widget-content ui-corner-bottom">
                content about Proudction here 
    </div>
    <div id="invoices" class="ui-tabs-panel ui-widget-content ui-corner-bottom">
                content about Invoices here
    </div>
    <div id="warranty" class="ui-tabs-panel ui-widget-content ui-corner-bottom">
                content about Warranty here
    </div>
    <div id="services" class="ui-tabs-panel ui-widget-content ui-corner-bottom">
                content about Services here
    </div>
    <div id="parts" class="ui-tabs-panel ui-widget-content ui-corner-bottom">
                content about Parts List here
    </div>
    <div id="memorandums" class="ui-tabs-panel ui-widget-content ui-corner-bottom">
                content about Memorandums here
    </div>
</div>

JS:有一些语法错误,你必须删除更多的类,并添加更多的类。

<script type="text/javascript">

$(document).ready(function(){
    $('#simpaleTabs div').hide();
    $('#simpaleTabs div:first').show();
    $('#simpaleTabs ul li:first').addClass('active');

    $('#simpaleTabs ul li a').click(function(){
        $('#simpaleTabs ul li').removeClass('ui-state-active').removeClass('ui-tabs-active').removeClass('active');
        $(this).parent("li").addClass('ui-state-active').addClass('ui-tabs-active').addClass('active');
        var currentTab = $(this).attr('href');
        $('#simpaleTabs div').hide();
        $(currentTab).show();
        return false;
    });
});
</script>