开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜