开发者

What are the non-library equivalents of jQuery's $('.foo', context)?

开发者 https://www.devze.com 2023-02-05 19:34 出处:网络
That i开发者_开发知识库s to say how can javascript find elements (eg. those with class \'foo\') within a passed subset of the document (\'context\', in the jQuery example).If context is a DOM element,

That i开发者_开发知识库s to say how can javascript find elements (eg. those with class 'foo') within a passed subset of the document ('context', in the jQuery example).


If context is a DOM element, I believe that it inherits all of the document's DOM methods. For example:

jQuery('.foo', context)

..could be rewritten as:

context.getElementsByClassName('foo')

..so long as context is a DOM element.


You can call someElem.getElementsByClassName.
Note that it's not supported by IE.


The DOM method getElementsByClassName()

var elements = context.getElementsByClassName( 'foo' );

Just be aware of its browser support.


context.getElementsByClass('foo');
context.querySelectorAll('.foo');

For some reason, IE8 supports querySelectorAll, but only IE9 supports getElementsByClassName.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号