我的代码是
<!DOCTYPE html>
<html ng-app="" ng-controller="myCtrl">
<body>
<button ng-click="AppendSample()">
Show
</button>
<div id="divID">
OK TEXT
</div>
<script>
function myCtrl($scope) {
$scope.AppendSample = function() {
var myElement = angular.element( document.querySelector( '#divID' ) );
alert(myElement);
}
}
</script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
</body>
</html>
我想提醒
ID 为divID
的div 元素,但弹出的提醒框是[[object HTMLDivElement]]
而不是确定文本
。我的问题在哪里?我如何获得适当的警报?谢谢。
最佳答案
您可以使用 angular 的 jQlite 函数。根据angular doc
jqLite is a tiny, API-compatible subset of jQuery that allows Angular to manipulate the DOM in a cross-browser compatible way. jqLite implements only the most commonly needed functionality with the goal of having a very small footprint.
确保你的 jQuery 在 angular.js 之前加载,然后你就可以简单地使用
$("#divID").html();
或
var myElement = angular.element( document.querySelector( '#divID' ) );
alert(myElement.html());
关于javascript - Angular JS 无法捕获 div 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30899662/