tabs-禁用mouseenter onclick直到mouseleave

时间:2013-01-28 21:41:53

标签: jquery tabs hover mouseenter mouseleave

如何更改js以便在选项卡单击(页面加载)之后隐藏选项卡内容,直到mouseleave选项卡?

以下是所有样本:

//<![CDATA[ 
$(window).load(function(){
$(document).ready(function() {

    $(".tab").click(function() {
        var tabId = $(this).attr('id');
        $("#" + tabId + "content").hide();
    });

    $(".tabcontent").click(function() {
        $(this).hide();
    });

    $(".tab").mouseenter(function() {
        var tabId = $(this).attr('id');
        $("#" + tabId + "content").show();
    });

    $(".tabcontent").mouseenter(function() {
        $(this).show();
    });

    $(".tab").mouseleave(function() {
        var tabId = $(this).attr('id');
        $("#" + tabId + "content").hide();
    });

    $(".tabcontent").mouseleave(function() {
        $(this).hide();
    });
});
});//]]>  

index.php和page1.php是相同的

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
  <title> - jsFiddle demo</title>

  <script type='text/javascript' src='http://code.jquery.com/jquery-1.4.2.js'></script>
  <script type='text/javascript' src='/Test/script.js'></script>

  <link rel="stylesheet" type="text/css" href="/Test/styles.css"/>

</head>
<body>

<div id="tabs">
    <ul>
        <li id="tab1" class="tab"><a href="/Test/page1.php">Tab 1</a></li>
    </ul>
</div>

<div id="tabcontents">
    <div id="tab1content" class="tabcontent">
        <p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. Donec sollicitudin mi sit amet mauris. Nam elementum quam ullamcorper ante. Etiam aliquet massa et lorem. Mauris dapibus lacus auctor risus. Aenean tempor ullamcorper leo. Vivamus sed magna quis ligula eleifend adipiscing. Duis orci. Aliquam sodales tortor vitae ipsum. Aliquam nulla. Duis aliquam molestie erat. Ut et mauris vel pede varius sollicitudin. Sed ut dolor nec orci tincidunt interdum. Phasellus ipsum. Nunc tristique tempus lectus.</p>
        <button>Button 1</button> <button>Button 2</button>
    </div>

</div>

<h1>Normal page content.</h1>

<p>Mauris eleifend est et turpis. Duis id erat. Suspendisse potenti. Aliquam vulputate, pede vel vehicula accumsan, mi neque rutrum erat, eu congue orci lorem eget lorem. Vestibulum non ante. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Fusce sodales. Quisque eu urna vel enim commodo pellentesque. Praesent eu risus hendrerit ligula tempus pretium. Curabitur lorem enim, pretium nec, feugiat nec, luctus a, lacus.</p>

<p>Duis cursus. Maecenas ligula eros, blandit nec, pharetra at, semper at, magna. Nullam ac lacus. Nulla facilisi. Praesent viverra justo vitae neque. Praesent blandit adipiscing velit. Suspendisse potenti. Donec mattis, pede vel pharetra blandit, magna ligula faucibus eros, id euismod lacus dolor eget odio. Nam scelerisque. Donec non libero sed nulla mattis commodo. Ut sagittis. Donec nisi lectus, feugiat porttitor, tempor ac, tempor vitae, pede. Aenean vehicula velit eu tellus interdum rutrum. Maecenas commodo. Pellentesque nec elit. Fusce in lacus. Vivamus a libero vitae lectus hendrerit hendrerit.</p>


</body>


</html>

的CSS:

#tabs {
    width: 400px;
}
#tabs ul {
    padding: 0;
    margin: 0;
    list-style: none;
}
#tabs ul li {
    float: left;
    padding: 10px;
    background: #ccc;
}
#tabcontents {
    clear: both;
}
.tabcontent {
    display: none;
    width: 400px;
    background: #aaa;
    position: absolute;
}

1 个答案:

答案 0 :(得分:0)

试试这个:

var currentUrl = window.location.href;

if( currentUrl === 'http://mydomain.com/Test/page1.php' ) {
    $('#tab1content').hide();
}else if ( currentUrl === 'http://mydomain.com/Test/page2.php' ) {
    $('#tab2content').hide();
}