javascript - Prevent element from getting focus - Stack Overflow

Essentially is it possible to stop an element, in my case a button element from getting focus in javasc

Essentially is it possible to stop an element, in my case a button element from getting focus in javascript?

What I am trying to achieve is an AngularJS directive that when it gets focus will show a number of buttons/UI elements. Using the ng-focus and ng-blur directives this is easily achieved however when I try to click any of the buttons, the button gains focus so the div no longer has focus, ng-blur is triggered and the buttons are hidden.

I want the buttons to stay visible whilst the directive element has focus and be clickable.

Here is a Plunker showing this behavior.

Thanks in advance.

Essentially is it possible to stop an element, in my case a button element from getting focus in javascript?

What I am trying to achieve is an AngularJS directive that when it gets focus will show a number of buttons/UI elements. Using the ng-focus and ng-blur directives this is easily achieved however when I try to click any of the buttons, the button gains focus so the div no longer has focus, ng-blur is triggered and the buttons are hidden.

I want the buttons to stay visible whilst the directive element has focus and be clickable.

Here is a Plunker showing this behavior.

Thanks in advance.

Share Improve this question asked May 5, 2014 at 0:51 adamKadamK 3,8895 gold badges38 silver badges49 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

I think it would have been simpler not to use any focusable element such as <button>s and <a> anchor tags. You can use a div that is not focusable and style it as a button, bind your ng event at that div and it should solve your problems. You gain the non-focusable advantage and still get to append your events in that particular element.

focusableDiv directive

change

<button class="btn" ng-click="add()" ng-show="isFocused">Action</button>

to

<div class="btn btn-primary" ng-click="add()" ng-show="isFocused">Action</div>

Note: the changes above assumes that you are using bootstrap-css to style your <div> button

See this plunker update

You know....you don't have to actually click the button.

'<button class="btn" ng-focus="add()" ng-show="isFocused">Action</button>' +

Fork of your plunkr: http://plnkr.co/edit/7uoOF5HS0yrsW5kquodA?p=preview

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745484085a4629687.html

相关推荐

  • javascript - Prevent element from getting focus - Stack Overflow

    Essentially is it possible to stop an element, in my case a button element from getting focus in javasc

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信