可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'd like to use a Twitter Bootstrap dropdown button:
<div class="btn-group">
<button class="btn dropdown-toggle" data-toggle="dropdown">Action <span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
</ul>
</div><!-- /btn-group -->
When the user changes the value of the button to 'Another action', instead of simply navigating to #
, I'd actually like to handle the action in a custom way.
But I can't see any event handlers in the dropdown plugin for doing this.
Is it actually possible to use Bootstrap dropdown buttons to handle user actions? I'm starting to wonder if they're only intended for navigation - it's confusing that the example gives a list of actions.
回答1:
Twitter bootstrap is meant to give a baseline functionality, and provides only basic javascript plugins that do something on screen. Any additional content or functionality, you'll have to do yourself.
<div class="btn-group">
<button class="btn dropdown-toggle" data-toggle="dropdown">Action <span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="#" id="action-1">Action</a></li>
<li><a href="#" id="action-2">Another action</a></li>
<li><a href="#" id="action-3">Something else here</a></li>
</ul>
</div><!-- /btn-group -->
and then with jQuery
jQuery("#action-1").click(function(e){
//do something
e.preventDefault();
});
回答2:
Try this:
$('div.btn-group ul.dropdown-menu li a').click(function (e) {
var $div = $(this).parent().parent().parent();
var $btn = $div.find('button');
$btn.html($(this).text() + ' <span class="caret"></span>');
$div.removeClass('open');
e.preventDefault();
return false;
});
回答3:
In Bootstrap 3 'dropdown.js' provides us with the various events that are triggered.
click.bs.dropdown
show.bs.dropdown
shown.bs.dropdown
etc
回答4:
Here is a working example of how you could implement custom functions for your anchors.
http://jsfiddle.net/CH2NZ/43/
You can attach an id to your anchor:
<li><a id="alertMe" href="#">Action</a></li>
And then use jQuery's click event listener to listen for the click action and fire you function:
$('#alertMe').click(function(e) {
alert('alerted');
e.preventDefault();// prevent the default anchor functionality
});
回答5:
I have been looking at this. On populating the drop down anchors, I have given them a class and data attributes, so when needing to do an action you can do:
<li><a class="dropDownListItem" data-name="Fred Smith" href="#">Fred</a></li>
and then in the jQuery doing something like:
$('.dropDownListItem').click(function(e) {
var name = e.currentTarget;
console.log(name.getAttribute("data-name"));
});
So if you have dynamically generated list items in your dropdown and need to use the data that isn't just the text value of the item, you can use the data attributes when creating the dropdown listitem and then just give each item with the class the event, rather than referring to the id's of each item and generating a click event.
回答6:
Anyone here looking for Knockout JS integration.
Given the following HTML (Standard Bootstrap dropdown button):
<div class="dropdown">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
Select an item
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li>
<a href="javascript:;" data-bind="click: clickTest">Click 1</a>
</li>
<li>
<a href="javascript:;" data-bind="click: clickTest">Click 2</a>
</li>
<li>
<a href="javascript:;" data-bind="click: clickTest">Click 3</a>
</li>
</ul>
</div>
Use the following JS:
var viewModel = function(){
var self = this;
self.clickTest = function(){
alert("I've been clicked!");
}
};
For those looking to generate dropdown options based on knockout observable array, the code would look something like:
var viewModel = function(){
var self = this;
self.dropdownOptions = ko.observableArray([
{ id: 1, label: "Click 1" },
{ id: 2, label: "Click 2" },
{ id: 3, label: "Click 3" }
])
self.clickTest = function(item){
alert("Item with id:" + item.id + " was clicked!");
}
};
<!-- REST OF DD CODE -->
<ul class="dropdown-menu" role="menu">
<!-- ko foreach: { data: dropdownOptions, as: 'option' } -->
<li>
<a href="javascript:;" data-bind="click: $parent.clickTest, text: option.clickTest"></a>
</li>
<!-- /ko -->
</ul>
<!-- REST OF DD CODE -->
Note, that the observable array item is implicitly passed into the
click function handler for use in the view model code.
回答7:
Without having to change your button group at all, you can do the following. Below, I assume your dropdown button
has an id
of fldCategory
.
<script type="text/javascript">
$(document).ready(function(){
$('#fldCategory').parent().find('UL LI A').click(function (e) {
var sVal = e.currentTarget.text;
$('#fldCategory').html(sVal + ' <span class="caret"></span>');
console.log(sVal);
});
});
</script>
Now, if you set the .btn-group
of this item itself to have the id
of fldCategory
, that's actually more efficient jQuery and runs a little faster:
<script type="text/javascript">
$(document).ready(function(){
$('#fldCategory UL LI A').click(function (e) {
var sVal = e.currentTarget.text;
$('#fldCategory BUTTON').html(sVal + ' <span class="caret"></span>');
console.log(sVal);
});
});
</script>