似乎AngularJS默认可以防止事件传播。 Here is a test on jsfiddle。如何在AngularJS中允许事件传播?
从上面的链接可以看出,覆盖链接的div
可以防止事件在其下传播div
,所以点击链接时链接不工作。这只是一个测试案例,在真实情况下,div
得到了其他功能,需要在那里,所以删除div
不是一个选项。
那么,如何让鼠标点击传播到div
下的链接呢?
代码万一你不想点击链接,或者链接在未来的某一天变成死链接。 (而且,以适应堆栈溢出的规则)
HTML
<div class="container">
<div class="unrelated">
<a href="http://stackoverflow.com">Stack overflow
</a><a href="http://www.goole.com">Google</a>
</div>
<div class="scope" ng-app="myApp" ng-controller="testing">
<div class="half-cover" ng-click="leftShow = false" ng-show="leftShow == true">
Click to remove one
</div><div class="half-cover" ng-click="rightShow = false" ng-show="rightShow == true">
Click to remove one
</div>
</div>
</div>
的Javascript
var myApp = angular.module('myApp',[]);
myApp.controller('testing', ['$scope', MyCtrlFunction]);
function MyCtrlFunction($scope) {
$scope.leftShow = true;
$scope.rightShow = true;
}
CSS
.container {
position: relative;
height: 300px;
width: 500px;
background-color: white;
}
.unrelated {
position: absolute;
width: 100%;
height: 100%;
}
.unrelated a{
display: inline-block;
width: 50%;
height: 100%;
line-height: 300px;
text-align: center;
}
.scope {
position: absolute;
left: 0;
top: 0;
height: 100%;
width: 100%;
z-index: 1000;
}
.half-cover {
display: inline-block;
height: 100%;
width: 50%;
background-color: black;
color: white;
line-height: 300px;
text-align: center;
}
正如我所提到的,在div在我的实际情况了功能。虽然你没有删除它,但是由于div失去了覆盖'div'的功能,所以使用'z-index:-1'也不好。 – cytsunny
@cytsunny您无法访问它下面的内容。你也可以通过使用'ng-class'类似的方法轻松地覆盖div – tanmay