在jQuery中使用子节点访问子节点

时间:2015-10-10 07:55:28

标签: javascript jquery angularjs

我有两个跨度,点击一个span标签,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()标记? Plunker是http://plnkr.co/edit/UnyqbY6lLRqhAb8183Nf?p=preview

2 个答案:

答案 0 :(得分:2)

请查看plunker

<!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>

答案 1 :(得分:1)

为什么不在这里使用纯Angular而不是使用jQuery。我不是100%确定你想要实现的目标,但我猜你想要在点击特定范围时切换p元素。如果是这样,您可以使用此类代码:

Statement statement = connection.createStatement();
try {
    statement.execute();
    // Other things you want to with statement...
} finally {
    statement.close();
}

HTML:

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
    }
});

以下是updated sample

相关问题