最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - angularjs show div if clicked inside a input field and hide if clicked outside - Stack Overflow

programmeradmin2浏览0评论

I want to show my div if user clicks inside the input field, and if clicked outside only, then it should hide it. If clicked inside the field again, it shouldn't hide. Here is my attempt: JSFIDDLE LINK

<div ng-app="app">
<div ng-controller="HelpCtrl">
    <input type="text" id="myText" name="myText" ng-click="showHelp = ! showHelp">
    
    <div  class="details" ng-class="{ 'hidden': ! showHelp }">
    <p>
    The help text here!
    </p>
    </div>
</div>
</div>

the problem is that when the page is opened, I see the help text, and it suddenly disappears and when I click inside the field, it shows again, but it disappears only when clicked inside the field again. Now I want it to hide only if clicked outside the field. Please help me with this.

I want to show my div if user clicks inside the input field, and if clicked outside only, then it should hide it. If clicked inside the field again, it shouldn't hide. Here is my attempt: JSFIDDLE LINK

<div ng-app="app">
<div ng-controller="HelpCtrl">
    <input type="text" id="myText" name="myText" ng-click="showHelp = ! showHelp">
    
    <div  class="details" ng-class="{ 'hidden': ! showHelp }">
    <p>
    The help text here!
    </p>
    </div>
</div>
</div>

the problem is that when the page is opened, I see the help text, and it suddenly disappears and when I click inside the field, it shows again, but it disappears only when clicked inside the field again. Now I want it to hide only if clicked outside the field. Please help me with this.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Aug 11, 2017 at 11:51 cpluscplus 1,1154 gold badges25 silver badges57 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Use ng-focus instead of ng-click

Plunker Example

<input type="text" ng-focus="focused = true" ng-blur="focused = false" />
<p ng-if="focused">Input has focus!</p>

below code should help you.

<style>

.hidden{
 -webkit-transition: width 2s; transition: width 2s;
 display:none !important;;

}
.details{
display:block;
}
</style>
    <div ng-app="app">
     <div ng-controller="HelpCtrl">
              <input type="text" id="myText" name="myText" ng-focus="showHelp = true" ng-blur="showHelp = false">

    <div  class="details" ng-class="{'hidden':!showHelp}">
    <p>
    The help text here!
    </p>
    </div>
      </div>
    </div>
发布评论

评论列表(0)

  1. 暂无评论