javascript - Angular JS 无法捕获 div 元素

标签 javascript angularjs

我的代码是

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

相关文章:

Javascript 如何返回包含奇数的数组

javascript - Angular 2 - Kendo UI 多选选项限制

javascript - qtip2 与 AngularJS 的基本使用

javascript - Angularjs无法识别 "this"

javascript - $.support.placeholder 在 jQuery 中意味着什么?

javascript - 在reactJS中 chop 文本

javascript - 将日期与 .valueOf 进行比较

javascript - AngularJS:在 ng-repeat 中为 div 设置相同的高度

java - 将 Angular JS 中的数据作为表单数据发布到 Java REST Web 服务时出现问题

javascript - 处理从 Angular $q.all 返回的结果