簡體   English   中英

在jQuery中使用子級訪問子級

[英]Accessing children using children in jQuery

我有兩個跨度,單擊一個跨度標簽后,應該顯示p標簽。 這是我的HTML文件:

<span class="span1" ng-click="show()">Span_1
<p class="p1" ng-show="var1">P_1</p>
</span>
<span class="span1" ng-click="show()">Span_2
<p class="p2" ng-show="var1">P_2</p>
</span>

相應的jQuery是

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.var1 = false;
    $("span").click(function(){
        $(this).children().toggle();
    })
});

Angularjs中也有任何children()標簽嗎? 柱塞為http://plnkr.co/edit/UnyqbY6lLRqhAb8183Nf?p=preview

請看看矮人

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

<div ng-app="myApp" ng-controller="myCtrl">

<span class="span1">Span_1
<p class="p1"  style="display:none;">P_1</p></span><br>
<span class="span1" >Span_2
<p class="p2" style="display:none;">P_2</p></span>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $("span").click(function(){
      console.log($(this).children())
      $(this).children().first().toggle();
    })
});
</script>

</body>
</html>

為什么不在這里使用純Angular而不是jQuery。 我不確定您要達到的目標是100%,但是我想您想在單擊特定范圍時切換p元素。 如果是這樣,您可以使用以下代碼:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.showChildren1 = false;    // show children of first span
    $scope.showChildren2 = false;    // show children of second span
    $scope.toggle1 = function(){
      $scope.showChildren1 = !$scope.showChildren1;  // toggle visibility
    }
    $scope.toggle2 = function(){
      $scope.showChildren2 = !$scope.showChildren2;   // toggle visibility
    }
});

HTML:

<div ng-app="myApp" ng-controller="myCtrl">
    <span class="span1" ng-click="toggle1()">Span_1</span><br>
    <p class="p1" ng-show="showChildren1">P_1</p>
    <span class="span1" ng-click="toggle2()">Span_2</span>
    <p class="p2" ng-show="showChildren2">P_2</p>
</div>

這是更新的示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM