Kinopyo Blog

プログラマとしてRuby, Rails, iPhone, iPad,Macなどなどと向き合う日々のログポース

jQuery siblings()とchildren()の内部ソースを見た

2010年04月19日 by kinopyo | jQuery


よく出来ていると思います。

勉強になります。

siblings: function( elem ) {
	return jQuery.sibling( elem.parentNode.firstChild, elem );
},
children: function( elem ) {
	return jQuery.sibling( elem.firstChild );
},

そしてjQuery.sibling()の中身です!

nodeTypeが1の場合はelementノードの意味です。

nはループに使われて、elemは除外する要素です。

例えばsiblings()の場合は自分自身を除外しています。

for文の書き方も覚えとこう、、

javaでのIteratorのhasNext()関数みたいな動きをしていますね。


	sibling: function( n, elem ) {
		var r = [];

		for ( ; n; n = n.nextSibling ) {
			if ( n.nodeType === 1 && n !== elem ) {
				r.push( n );
			}
		}

		return r;
	}

Tags: ,

You can leave a response, or trackback from your own site.

関連記事

  • http://www.choffy.net/crio-bru-chocolate-will-never-be-the-same/ coffee

    coffee…

    [...]jQuery siblings()とchildren()の内部ソースを見た | Kinopyo Blog[...]…