Javascript 如何在调用onClick函数时隐藏delete按钮
我正在编写一个代码,我需要有一个删除选项 当您单击列出的任何号码时,您将获得和(按钮)选项以删除。但我无法在单击按钮后隐藏(删除按钮) 你能告诉我怎么做吗Javascript 如何在调用onClick函数时隐藏delete按钮,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,我正在编写一个代码,我需要有一个删除选项 当您单击列出的任何号码时,您将获得和(按钮)选项以删除。但我无法在单击按钮后隐藏(删除按钮) 你能告诉我怎么做吗 {{e}}删除 我假设您只想在单击“删除”按钮后隐藏它(当可见时) 更改您单击以切换selectedIndex值 ng-click="$parent.selectedIndex!=$index?$parent.selectedIndex=$index:$parent.selectedIndex = null" 不使用$pare
-
{{e}}删除
我假设您只想在单击“删除”按钮后隐藏它(当可见时)
更改您单击以切换selectedIndex
值
ng-click="$parent.selectedIndex!=$index?$parent.selectedIndex=$index:$parent.selectedIndex = null"
不使用
$parent
符号的清洁剂溶液
<p>click to select:</p>
<ul>
<li ng-repeat="e in arr track by $index">
<span ng-click="changeIndex($index)">{{ e }}
<button ng-show="selectedIndex == $index">delete</button></button>
</span>
</li>
</ul>
虽然在作用域变量之前使用$parent
anotation会在变量之间创建紧密耦合,但我更希望您在定义角度或模型时使用点规则
,另一个最好的方法是使用followcontrollerAs
模式
定义模型时。只需在按钮中添加一个状态变量即可 将此添加到控制器
$scope.buttonClicked=false;
$scope.toggleButton = function(e) {
$scope.buttonClicked=!$scope.buttonClicked;
e.stopPropagation()
}
然后
{{e}}删除
e、 停止传播()是必需的,因为当您单击按钮时,单击事件也会传播到span和li标记,并且设置按钮再次单击为false。您有两个
,如果使用ng而不是ng show
,则使用ng,以及如何执行@IvanYes@gianlucatursi,但这无法解决问题。您想隐藏删除,但在删除时单击按钮?您的代码可以正常工作,但有一个问题。对于第一个列表,删除按钮不会隐藏。如果这样做行得通的话,我愿意接受你的回答@潘卡吉Parkar@User123这是因为当我这样做时,$index 0
倾向于变成1
(这是真的)!0
$scope.arr = [1,2,3,4,5];
$scope.selectedIndex = undefined;
$scope.changeIndex = function(index){
return $scope.selectedIndex != index? $scope.selectedIndex=index:$scope.selectedIndex = null
}
$scope.buttonClicked=false;
$scope.toggleButton = function(e) {
$scope.buttonClicked=!$scope.buttonClicked;
e.stopPropagation()
}
<span ng-click="$parent.selectedIndex=$index; $parent.buttonClicked=false">{{ e }} <button ng-show="$parent.selectedIndex == $index && !$parent.buttonClicked" ng-click="$parent.toggleButton($event)">delete</button></span>