mirror of
https://github.com/twbs/bootstrap.git
synced 2025-02-27 05:12:29 +01:00
Merge branch 'kasperp-tab-event'
This commit is contained in:
commit
d2de00f1df
@ -357,6 +357,27 @@ $('#my-modal').bind('hidden', function () {
|
||||
})
|
||||
</script></pre>
|
||||
</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>
|
||||
<ul class="tabs" data-tabs="tabs" >
|
||||
<li class="active"><a href="#home">Home</a></li>
|
||||
|
12
js/bootstrap-tabs.js
vendored
12
js/bootstrap-tabs.js
vendored
@ -27,21 +27,27 @@
|
||||
|
||||
function tab( e ) {
|
||||
var $this = $(this)
|
||||
, href = $this.attr('href')
|
||||
, $ul = $this.closest('ul')
|
||||
, $controlled
|
||||
, href = $this.attr('href')
|
||||
, previous
|
||||
|
||||
if (/^#\w+/.test(href)) {
|
||||
e.preventDefault()
|
||||
|
||||
if ($this.hasClass('active')) {
|
||||
if ($this.parent('li').hasClass('active')) {
|
||||
return
|
||||
}
|
||||
|
||||
previous = $ul.find('.active a')[0]
|
||||
$href = $(href)
|
||||
|
||||
activate($this.parent('li'), $ul)
|
||||
activate($href, $href.parent())
|
||||
|
||||
$this.trigger({
|
||||
type: 'change'
|
||||
, relatedTarget: previous
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
44
js/tests/unit/bootstrap-tabs.js
vendored
44
js/tests/unit/bootstrap-tabs.js
vendored
@ -11,39 +11,67 @@ $(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><a href="#profile">Profile</a></li>'
|
||||
+ '</ul>'
|
||||
+ '</ul>')
|
||||
|
||||
|
||||
$('<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")
|
||||
|
||||
$(tabsHTML).tabs().find('a').first().click()
|
||||
$tabsHTML.tabs().find('a').first().click()
|
||||
equals($("#qunit-runoff").find('.active').attr('id'), "home")
|
||||
|
||||
$("#qunit-runoff").empty()
|
||||
})
|
||||
|
||||
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><a href="#profile">Profile</a></li>'
|
||||
+ '</ul>'
|
||||
+ '</ul>')
|
||||
|
||||
|
||||
$('<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")
|
||||
|
||||
$(pillsHTML).pills().find('a').first().click()
|
||||
$pillsHTML.pills().find('a').first().click()
|
||||
equals($("#qunit-runoff").find('.active').attr('id'), "home")
|
||||
|
||||
$("#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)
|
||||
})
|
||||
|
||||
})
|
Loading…
x
Reference in New Issue
Block a user