gpt4 book ai didi

html - 将 ng-click 绑定(bind)到一个 css 类

转载 作者:太空宇宙 更新时间:2023-11-03 21:13:25 24 4
gpt4 key购买 nike

我们可以将 ng-click 绑定(bind)到 CSS 类吗?

我有很多 <td> s 绑定(bind)并检查它们是否被单击,然后触发其中标签的单击,以便为触摸设备提供最大的可单击区域。

我做了类似下面的事情。

var myApp = angular.module("myApp", [])
.controller("myController", ["$scope", "$timeout", function myController($scope, $timeout) {
$scope.checkRadio = function checkRadio($event) {
$timeout(function() {
angular.element($event.target).find("label").trigger('click');
}, 100);
};
}]);
.invisible-radio {
position: absolute;
display: inline;
opacity: 0;
width: 0;
margin: 0;
-webkit-appearance: none;
overflow: hidden;
}

.custom-radio-label {
display: inline-block;
position: relative;
padding-left: 20px;
vertical-align: text-top;
line-height: 20px;
cursor: pointer;
border-style: solid;
border-width: 0;
}

.custom-radio-label::before {
border-color: #7f7f7f;
border-radius: 50%;
background-color: #fff;
border-width: 2px;
content: "";
position: absolute;
top: 0;
left: 0;
width: 18px;
height: 18px;
border-style: solid;
-webkit-box-sizing: content-box;
-moz-box-sizing: content-box;
box-sizing: content-box;
}

.invisible-radio:checked+.custom-radio-label::after {
background-color: #337ab7;
border-radius: 50%;
top: 5px;
left: 5px;
content: "";
width: 12px;
height: 12px;
position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<table class="table table-hover text-center center-block" ng-app="myApp" ng-controller="myController">
<thead>
<tr>
<th>Option 1</th>
<th>Option 2</th>
</tr>
</thead>
<tbody>
<tr>
<td ng-click="checkRadio($event)">
<input id="option1" type="radio" class="invisible-radio" name="option" value="0" ng-model="model.Option" required />
<label class="custom-radio-label" for="option1"></label>
</td>
<td ng-click="checkRadio($event)">
<input id="option2" type="radio" class="invisible-radio" name="option" value="1" ng-model="model.Option" required />
<label class="custom-radio-label" for="option2"></label>
</td>
</tr>
</tbody>
</table>

但是去装饰 100 个 <td> 似乎有些过分了。与 ng-click属性。

我在想是否可以绑定(bind) ng-click我所有的<td>分享。

jQuery当然可以做到如下所示。

$(".someclassOnMyTD").click($event) {
$event.target.find("label").click();
}

但我正在寻找一种 Angular 方式。可能是指令。

最佳答案

您可以使用 angular.element 来完成

  var cells = angular.element(document.querySelectorAll(".someclassOnMyTD"));
cells.on('click', function(event){
// write your code here
// var labels = angular.element(event.currentTarget).find('label');
})

关于html - 将 ng-click 绑定(bind)到一个 css 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44406995/

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