1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-08-16 10:34:07 +02:00

Merge branch 'kasperp-tab-event'

This commit is contained in:
Jacob Thornton
2011-09-29 23:01:06 -07:00
3 changed files with 67 additions and 12 deletions

View File

@@ -357,6 +357,27 @@ $('#my-modal').bind('hidden', function () {
}) })
&lt;/script&gt;</pre> &lt;/script&gt;</pre>
</p> </p>
<h3>Events</h3>
<table class="zebra-striped">
<thead>
<tr>
<th style="width: 150px;">Event</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>change</td>
<td>This event fires on tab change. Use <code>event.target</code> and <code>event.relatedTarget</code> to target the active tab and the previous active tab respectively.</td>
</tr>
</tbody>
</table>
<pre class="prettyprint linenums">
$('#.tabs').bind('change', function (e) {
e.target // activated tab
e.relatedTarget // previous tab
})</pre>
<h3>Demo</h3> <h3>Demo</h3>
<ul class="tabs" data-tabs="tabs" > <ul class="tabs" data-tabs="tabs" >
<li class="active"><a href="#home">Home</a></li> <li class="active"><a href="#home">Home</a></li>

14
js/bootstrap-tabs.js vendored
View File

@@ -27,21 +27,27 @@
function tab( e ) { function tab( e ) {
var $this = $(this) var $this = $(this)
, href = $this.attr('href')
, $ul = $this.closest('ul') , $ul = $this.closest('ul')
, $controlled , href = $this.attr('href')
, previous
if (/^#\w+/.test(href)) { if (/^#\w+/.test(href)) {
e.preventDefault() e.preventDefault()
if ($this.hasClass('active')) { if ($this.parent('li').hasClass('active')) {
return return
} }
previous = $ul.find('.active a')[0]
$href = $(href) $href = $(href)
activate($this.parent('li'), $ul) activate($this.parent('li'), $ul)
activate($href, $href.parent()) activate($href, $href.parent())
$this.trigger({
type: 'change'
, relatedTarget: previous
})
} }
} }
@@ -59,4 +65,4 @@
$('body').tabs('ul[data-tabs] li > a, ul[data-pills] > li > a') $('body').tabs('ul[data-tabs] li > a, ul[data-pills] > li > a')
}) })
}( window.jQuery || window.ender ); }( window.jQuery || window.ender );

View File

@@ -11,39 +11,67 @@ $(function () {
}) })
test("should activate element by tab id", function () { test("should activate element by tab id", function () {
var tabsHTML = '<ul class="tabs">' var $tabsHTML = $('<ul class="tabs">'
+ '<li class="active"><a href="#home">Home</a></li>' + '<li class="active"><a href="#home">Home</a></li>'
+ '<li><a href="#profile">Profile</a></li>' + '<li><a href="#profile">Profile</a></li>'
+ '</ul>' + '</ul>')
$('<ul><li id="home"></li><li id="profile"></li></ul>').appendTo("#qunit-runoff") $('<ul><li id="home"></li><li id="profile"></li></ul>').appendTo("#qunit-runoff")
$(tabsHTML).tabs().find('a').last().click() $tabsHTML.tabs().find('a').last().click()
equals($("#qunit-runoff").find('.active').attr('id'), "profile") equals($("#qunit-runoff").find('.active').attr('id'), "profile")
$(tabsHTML).tabs().find('a').first().click() $tabsHTML.tabs().find('a').first().click()
equals($("#qunit-runoff").find('.active').attr('id'), "home") equals($("#qunit-runoff").find('.active').attr('id'), "home")
$("#qunit-runoff").empty() $("#qunit-runoff").empty()
}) })
test("should activate element by pill id", function () { test("should activate element by pill id", function () {
var pillsHTML = '<ul class="pills">' var $pillsHTML = $('<ul class="pills">'
+ '<li class="active"><a href="#home">Home</a></li>' + '<li class="active"><a href="#home">Home</a></li>'
+ '<li><a href="#profile">Profile</a></li>' + '<li><a href="#profile">Profile</a></li>'
+ '</ul>' + '</ul>')
$('<ul><li id="home"></li><li id="profile"></li></ul>').appendTo("#qunit-runoff") $('<ul><li id="home"></li><li id="profile"></li></ul>').appendTo("#qunit-runoff")
$(pillsHTML).pills().find('a').last().click() $pillsHTML.pills().find('a').last().click()
equals($("#qunit-runoff").find('.active').attr('id'), "profile") equals($("#qunit-runoff").find('.active').attr('id'), "profile")
$(pillsHTML).pills().find('a').first().click() $pillsHTML.pills().find('a').first().click()
equals($("#qunit-runoff").find('.active').attr('id'), "home") equals($("#qunit-runoff").find('.active').attr('id'), "home")
$("#qunit-runoff").empty() $("#qunit-runoff").empty()
}) })
test( "should trigger change event on activate", function () {
var $tabsHTML = $('<ul class="tabs">'
+ '<li class="active"><a href="#home">Home</a></li>'
+ '<li><a href="#profile">Profile</a></li>'
+ '</ul>')
, $target
, count = 0
, relatedTarget
, target
$tabsHTML
.tabs()
.bind( "change", function (e) {
target = e.target
relatedTarget = e.relatedTarget
count++
})
$target = $tabsHTML
.find('a')
.last()
.click()
equals(relatedTarget, $tabsHTML.find('a').first()[0])
equals(target, $target[0])
equals(count, 1)
})
}) })