当我点击输入文字时,我会打开一个下拉列表。因此,行为应该与打开它的按钮相同。我创建了一个jsfiddle here
这是我使用的代码:
<div ng-app="myApp">
<div class="uk-width-1-1" ng-controller="TestCtrl">
<form id="form" class="uk-form uk-width-1-1 center-form">
<div class="uk-button-group uk-width-1-2 uk-vertical-align-middle">
<!-- This is a button -->
<input id="input" type="text" placeholder="text" class="uk-width-1-1 uk-vertical-align-middle">
<!-- This is the container enabling the JavaScript -->
<div data-uk-dropdown="{mode:'click', justify:'#input'}">
<!-- This is the button toggling the dropdown -->
<a href="" style="vertical-align: middle!important;" class="uk-button"><i class="uk-icon-caret-down"></i></a>
<!-- This is the dropdown -->
<div class="uk-dropdown uk-dropdown-small">
<ul class="uk-nav uk-nav-dropdown" ng-repeat="item in items">
<li><a href="">{{item}}</a></li>
</ul>
</div>
</div>
</div>
</form>
</div>
</div>
我尝试了一些有角度的但它不起作用
var myApp = angular.module('myApp', []);
myApp.controller('TestCtrl',function($scope){
$scope.items=['menu item 1','menu item 2'];
$scope.itemClicked = function(item, event, index){
}
$(document).on('click', '.uk-dropdown', function() {
$(this).parents('[data-uk-dropdown]:first').removeClass('uk-open');
});
$(document).on('click', '#input', function() {
$(this).parents('[data-uk-dropdown]:first').addClass('uk-open');
});
});
当然,当您在输入中写入时,如果下拉列表打开,则不能关闭。
答案 0 :(得分:1)
$(this)
处理程序中的 #input
不是您的想法。您可以使用.uk-dropdown
。 focus
应使用textbox
。
$(document).on('click', '.uk-dropdown', function () {
$(this).closest('[data-uk-dropdown]:first').removeClass('uk-open');
});
$(document).on('focus', '#input', function () {
$('.uk-dropdown').closest('[data-uk-dropdown]:first').addClass('uk-open');
}).on('blur', '#input', function() {
$('.uk-dropdown').closest('[data-uk-dropdown]:first').removeClass('uk-open');
});