AngularJS - 获取元素属性值 [英] AngularJS - get element attributes values
本文介绍了AngularJS - 获取元素属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取元素属性值?
例如HTML 元素:
<button data-id="345" ng-click="doStuff($element.target)">Button</button>
JS:
function doStuff(item){
angular.element(item)[0].data('id'); // undefined is not a function
}
非常感谢任何建议,JSFIDDLE 演示在这里:http://jsfiddle.net/h3TFy/
Any suggestions much appreciated, JSFIDDLE demo here:http://jsfiddle.net/h3TFy/
推荐答案
由于您将目标元素发送到您的函数,您可以这样做以获取 id:
Since you are sending the target element to your function, you could do this to get the id:
function doStuff(item){
var id = item.attributes['data-id'].value; // 345
}
这篇关于AngularJS - 获取元素属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文