0

How to change js so tab content will be hidden after tab click(page load) until mouseleave tab?

Here is all sample:

//<![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 and page1.php are the same

<!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;
}
Zevi Sternlicht
  • 5,399
  • 19
  • 31
Nina
  • 31
  • 7
  • Please create a [jsFiddle](http://jsfiddle.net/) for us to better understand to problem. – Barlas Apaydin Jan 28 '13 at 21:47
  • http://jsfiddle.net/Nina1988/4CHAt/, index.php and page1.php have the same code, but click on tab and open new page we can't simulate in jsfiddle... maybe you can use wampserver to simulate the problem – Nina Jan 28 '13 at 21:56

1 Answers1

0

Try this:

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();
}
Barlas Apaydin
  • 7,233
  • 11
  • 55
  • 86
  • Is this solution work for you? For me unfortunately not...It is like there is some conflict because of mouseenter when page is loaded, I guess... – Nina Jan 28 '13 at 22:19
  • That what I want works if you put text instead link...
  • Tab 1
  • Link is making problem, and I need a link :) – Nina Jan 28 '13 at 22:43
  • @Nina I've tried everthing, but i cound't understand your problem. I'm gonna sleep now, i'll read your question tomorrow again. – Barlas Apaydin Jan 28 '13 at 23:04
  • My code is correct, I discover that the mentioned functionality (problem) depend on browser. But additionally I used [link](http://cherne.net/brian/resources/jquery.hoverIntent.html). – Nina Mar 08 '13 at 11:37