Javascript 如何使ng click仅对click事件有效
我不熟悉angular,尝试在单击当前链接时切换类。 但点击后,它会在所有链接上工作。我希望它只在当前元素上工作。 为此,我们在jquery中使用(这个) 脚本:Javascript 如何使ng click仅对click事件有效,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,我不熟悉angular,尝试在单击当前链接时切换类。 但点击后,它会在所有链接上工作。我希望它只在当前元素上工作。 为此,我们在jquery中使用(这个) 脚本: var data = '<td id="'+index+'" class="drag drop"><div class="ui-resizable tac"><div class="ui-resizable">' + header[index].description + '<br>'
var data = '<td id="'+index+'" class="drag drop"><div class="ui-resizable tac"><div class="ui-resizable">' + header[index].description + '<br>' + header[index].name +'</div></div><div id="div'+index+'" class="report-container" style="display:inline-block;float:left;"><ul class="report-list">';
for( var key = 0; key < listTemp.length; key ++){
data+= "<li class='bg-l-grey' ng-class='{ opened: selectedIndex == 0}' style='background:" + listTemp[key].color +"'><span><em class='left'>" + listTemp[key].mpValue + "</em><em class='right'>" + parseInt(listTemp[key].yield) + "</em></span>"+
'<div class="list-swiper bg-black" ng-click="selectedIndex = 0"><span class="swipe-left"></span><span class="swipe-right"></span></div><div class="report-icon-list bg-l-green"><a href="#" class="cht-doc"><span><i class="cht-sprite"></i></span></a><a href="#" class="cht-cam"><span><i class="cht-sprite"></i></span></a><a href="#" class="cht-add"><span><i class="cht-sprite"></i></span></a></div></li>';
}
data+= '</ul></div></td>';
$scope.openSwap = function($event) {
// body...
var elementParent = $event.currentTarget.parentElement.offsetParent;
angular.element(elementParent).toggleClass("opened");
if ($(elementParent).hasClass("opened")) {
}else {
console.log(false);
}
$event.stopPropagation();
}
var数据=''+标题[索引]。描述+'
'+标题[索引]。名称+';
对于(var key=0;key“+listemp[key].mpValue+”“+parseInt(listemp[key].yield)+”+
“”;
}
数据+='
';
$scope.openSwap=函数($event){
//身体。。。
var elementParent=$event.currentTarget.parentElement.offsetParent;
元素(elementParent).toggleClass(“打开”);
if($(elementParent).hasClass(“打开”)){
}否则{
console.log(false);
}
$event.stopPropagation();
}
我想单击“.list swiper”类上的事件和父级li上的类切换。使用
ng class
更改此特定链接上的类
<ANY class="ng-class: expression;"> ... </ANY>
。。。
您可以切换一个类并使用ng类
:
<div class="list-swiper bg-black"
ng-class="{ opened: thisElementClicked }"
ng-click="thisElementClicked = true">..</div>
您可能有多个li
元素,您可能希望更动态地执行此操作:
<ul>
<li ng-class="{ opened: selectedIndex == 0 }">
<div class="list-swiper bg-black"
ng-click="openSwap(0)">..</div>
</li>
<li ng-class="{ opened: selectedIndex == 1 }">
<div class="list-swiper bg-black"
ng-click="openSwap(1)">..</div>
</li>
</ul>
$scope.swipers = [
{ title: "first swiper"},
{ title: "second swiper"},
// ..
}
$scope.openSwap = function (index) {
$scope.selectedIndex = index;
// or toggle, depends what you want:
// if ($scope.selectedIndex == index) {
// $scope.selectedIndex = -1;
// } else {
// $scope.selectedIndex = index;
// }
}
更简单的是,如果您可以动态构建
li
:
<ul>
<li ng-class="{ opened: selectedIndex == 0 }">
<div class="list-swiper bg-black"
ng-click="openSwap(0)">..</div>
</li>
<li ng-class="{ opened: selectedIndex == 1 }">
<div class="list-swiper bg-black"
ng-click="openSwap(1)">..</div>
</li>
</ul>
$scope.swipers = [
{ title: "first swiper"},
{ title: "second swiper"},
// ..
}
$scope.openSwap = function (index) {
$scope.selectedIndex = index;
// or toggle, depends what you want:
// if ($scope.selectedIndex == index) {
// $scope.selectedIndex = -1;
// } else {
// $scope.selectedIndex = index;
// }
}
视图:
-
{{swiper.title}
“jqLite”(在angular.element页面上定义)提供了DOM遍历方法,如子对象()
,父对象()
,内容()
,查找()
,下一个()
(但不是上一个())。没有类似选择器的方法
您可以尝试core JavaScript的querySelector,如下链接:
在jquery中,angular中没有任何$(this)概念,相反,您必须通过javascripts querySelector或angular.element(document.find(…)或$document.find()遍历元素直到元素
有关文档,请查看下面的签出链接:
这应该是一个评论。这个地方是为了帮助别人,而不是批评其他试图帮助别人的人。这是一个对你有帮助的评论,可以避免投反对票。我想单击事件“.list swiper”类和家长李上的类切换。^^谢谢你的帮助。使用您提供的代码,但没有帮助。类没有在clickYou添加了一些代码,我明白为什么这样做不起作用。在那里,您将索引硬编码为0,而使用
for
-loop键
变量作为索引。或者,如果使用angular,您可能应该重写整个内容,而不应该在这里构建DOM。你能在你的工作中创建一个小的plunker/jsfidle吗?
<ul>
<li ng-repeat="swiper in swipers"
ng-class="{ opened: selectedIndex == $index }">
<div class="list-swiper bg-black"
ng-click="openSwap($index)">{{ swiper.title }}</div>
</li>
</ul>