Created
February 10, 2014 05:26
-
-
Save RyoSugimoto/8910793 to your computer and use it in GitHub Desktop.
jQueryを使ってテキストノードのみを抽出する。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// テキストノードのみを抽出して返す | |
function getTextNode ($target, str) { | |
var nodes = $target | |
.contents() | |
.filter(function () { | |
return this.nodeType === 3 // テキストノードか否か | |
&& /\S/.test(this.data) // 空白か否か | |
&& $.inArray($(this).parent(), $target) // 直下か否か | |
&& (typeof str === 'undefined' || str === this.nodeValue); // 文字列の指定がある場合 | |
}); | |
return nodes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment