如何使用jqLite通过类名选择元素? [英] How to select an element by classname using jqLite?
问题描述
我正在尝试从Angular.js应用中删除jquery,以使其更轻巧,而改用Angular的jqLite.但是该应用程序大量使用了jqLite不支持的find('#id')和find('.classname'),仅使用了标签名称"(根据文档)
I'm trying to remove jquery from my Angular.js app in order to make it lighter, and put Angular's jqLite instead. But the app makes heavy use of find('#id') and find ('.classname'), which are not supported by jqLite, only 'tag names' (as per documentation)
想知道您认为改变它的最佳方法是什么.我考虑过的一种方法是创建自定义HTML标签.例如:
更改
<span class="btn btn-large" id="add-to-bag">Add to bag</span>
wondered what do u feel would be the best approach to change it. One approach I thought about is to create custom HTML tags. for example:
change
<span class="btn btn-large" id="add-to-bag">Add to bag</span>
到
<a2b style="display:none;"><span class="btn btn-large" >Add to bag</span></a2b>
和
$element.find('#add-to-bag')
到
$element.find('a2b')
有什么想法吗?其他想法?
Any thoughts? other ideas?
谢谢
骗子
推荐答案
本质上,由@ kevin-b指出:
Essentially, and as-noted by @kevin-b:
// find('#id')
angular.element(document.querySelector('#id'))
//find('.classname'), assumes you already have the starting elem to search from
angular.element(elem.querySelector('.classname'))
Note: If you're looking to do this from your controllers you may want to have a look at the "Using Controllers Correctly" section in the developers guide and refactor your presentation logic into appropriate directives (such as <a2b ...>).
这篇关于如何使用jqLite通过类名选择元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!