gpt4 book ai didi

javascript - 如何使按钮可点击但其父div?

转载 作者:行者123 更新时间:2023-12-02 13:55:04 24 4
gpt4 key购买 nike

我试图在单击 div 内的按钮时触发一个函数,并在单击 div 时触发另一个函数。当我单击 button div 时,也是如此单击按钮时如何使 div 不可单击

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div style="width:400px;height:400px;background:blue" ng-app="myApp" ng-controller="myCtrl" ng-click="divC()">


<button ng-click="myfun()" style="float:right;top:0">
CLICK
</button>


</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.count = 0;
$scope.myfun = function(){
alert("button")
};
$scope.divC = function(){
alert("div")
};
});
</script>

</body>
</html>

最佳答案

您将需要使用event.stopPropagation(),这将阻止点击冒泡到其容器。

https://developer.mozilla.org/en/docs/Web/API/Event/stopPropagation

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div style="width:400px;height:400px;background:blue" ng-app="myApp" ng-controller="myCtrl" ng-click="divC()">


<button ng-click="myfun($event)" style="float:right;top:0">
CLICK
</button>


</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.count = 0;
$scope.myfun = function(event){
event.stopPropagation();
alert("button")
};
$scope.divC = function(){
alert("div")
};
});
</script>

</body>
</html>

关于javascript - 如何使按钮可点击但其父div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40752358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com