當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


AngularJS ng-mousedown用法及代碼示例

當特定HTML元素上發生mousedown事件時,AngularJS中的ng-mousedown指令用於應用自定義行為。當按下鼠標按鈕時,它可用於顯示彈出警報。所有HTML元素都支持它。

用法:

 <element ng-mousedown="expression"> Contents... </element> 

範例1:本示例使用ng-mousedown指令在單擊鼠標後設置元素的樣式。


<!DOCTYPE html> 
<html> 
      
<head> 
    <script src= 
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"> 
    </script> 
      
    <title>ng-mousedown Directive</title> 
</head> 
  
<body ng-app style="text-align:center"> 
      
    <h1 style="color:green">GeeksforGeeks</h1> 
      
    <h2>ng-mousedown Directive</h2> 
      
    <div> 
        <p ng-mousedown="geek={'color':'green', 
                'font-size':'larger'}" ng-mouseup= 
                "geek={'font-size':''}" ng-style="geek"
                ng-class="'button'"> 
            Hold mouse click to see effect. 
        </p> 
    </div> 
</body> 
  
</html>

輸出:
在單擊元素之前:
ngmousedown
單擊元素後:
ngmousedown

範例2:本示例使用ng-mousedown指令在單擊輸入區域後顯示警報消息。

<!DOCTYPE html> 
<html> 
      
<head> 
    <script src= 
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"> 
    </script> 
      
    <title>ng-mousedown Directive</title> 
</head> 
  
<body  ng-app="app" style="text-align:center"> 
      
    <h1 style="color:green">GeeksforGeeks</h1> 
      
    <h2>ng-mousedown Directive</h2> 
   
    <div ng-controller="app"> 
        Input:<input type="text" ng-mousedown="alert()"
            ng-model="click" /> 
    </div> 
      
    <script> 
        var app = angular.module("app", []); 
        app.controller('app', ['$scope', function ($scope) { 
            $scope.click = 'geeksforgeeks'; 
            $scope.alert = function () { 
                alert($scope.click); 
            } 
        }]); 
    </script> 
</body> 
  
</html>

輸出:
單擊輸入文本之前:
ngmousedown
單擊輸入文本後:
ngmousedown



相關用法


注:本文由純淨天空篩選整理自Vishal Chaudhary 2大神的英文原創作品 AngularJS | ng-mousedown Directive。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。