繁体   English   中英

Angularjs $ document [0] .activeElement而不是$ document.activeElement

[英]Angularjs $document[0].activeElement instead of $document.activeElement

我正在使用angularjs编写占位符指令。

在单击处理程序上,我想检查元素和document.activeElement是否相同。

我尝试使用$docuemnt.activeElement ,但它始终undefined 但是当我使用$document[0].activeElement我得到了当前活动的元素。

$document[0].activeElement是访问当前活动元素的正确方法吗? 或者我做错了什么?

不, $document是包装版本document ,它使用jQlite这是jQuery的一个微小的版本,因此包裹$document没有叫任何方法activeElement因为document是内部$document ,所以你必须使用

$document[0].activeElement

要么

document.activeElement

您还可以创建一个全局变量,它是activeElement的包装版本,如此。

var $activeElement = angular.element(document.activeElement);
$activeElement.attr('focused', 'yes'); // Example usage

正如另外一个注意事项,您使用上述答案:

$document[0].activeElement

那么你实际上是一个活动元素数组,所以你需要使用:

$document[0].activeElement[0]

要么

angular.element($document[0].activeElement)[0]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM