how to highlight selected jquery ui tab?

2019-08-05 06:46发布

I've learned how to create a jquery ui tab, thanks to http://jqueryui.com/tabs, and customized the look of it. I've been searching the web on how to highlight a selected tab by changing it's background color when someone clicks on it. So far, it's been frustrated and unsuccessful.

Here's the HTML Code Use:

<!DOCTYPE html>
<head>
<link href="style.css" rel="stylesheet" type="text/css" >

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.js"></script>

 <script>
$(function() {
$( "#tabs" ).tabs();
});
</script>

<title>Jquery Tab Test</title>
</head>

<body>

<div id="tab-wrapper">
<div id="tabs">

<ul>
    <li><a href="#tabs1">Tab 1</a></li>
    <li><a href="#tabs2">Tab 2</a></li>
    <li><a href="#tabs3">Tab 3</a></li>
</ul>

<br class="clearboth" />

<div id="tabs1" class="tab-content">
Content 1
</div>

<div id="tabs2" class="tab-content">
Content 2
</div>

<div id="tabs3" class="tab-content tabs3">
Content 3
</div>

</div>
</div>

</body>

and CSS:

body, html {
    height: 101%;
}

body {
    font-family: arial;
}

.clearboth {
    clear: both;
    padding: 0;
    margin: 0;
}
#tab-wrapper {
    background-color: #f00fff;
    border: solid 1px #909090;
    padding: 5px;
    width: 330px;
}

#tabs {
    overflow: hidden;
}

#tabs ul li,
#tabs ul { 
    margin: 0;
    padding: 0;
    list-style: none;
}

#tabs ul a {
    float: left;
    padding: 5px;
    display: block;
    width: 100px;
    text-align: center;
    text-decoration: none;
    color: #000000;
    height: 20px;
    line-height: 20px;
    font-size: 10pt;
    font-weight: bold;
    background-color: #cccccc;
}

#tabs ul a:hover {
    background-color: #f1f1f1;
}

#tabs .tab-content {
    padding: 5px;
    display: block;
    background-color: #f1f1f1;
    font-size: 10pt;
    height: 300px;
    border-top: solid 1px #000000;
}

Please help if anyone can.

2条回答
一纸荒年 Trace。
2楼-- · 2019-08-05 07:22

Jquery assigns the selected tab the class="ui-tabs-active" so to style it simply hook your css into it as follows:

#tabs .ui-tabs-active {
  background: yellow;
}
查看更多
对你真心纯属浪费
3楼-- · 2019-08-05 07:23

This works for me:

#tabs .ui-tabs-active {
  background: yellow;
}

but the above answer did not - the order in the css file is important - the above is after hover.

查看更多
登录 后发表回答