Skip to content

Instantly share code, notes, and snippets.

@RyoSugimoto
Created February 10, 2014 05:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RyoSugimoto/8910793 to your computer and use it in GitHub Desktop.
Save RyoSugimoto/8910793 to your computer and use it in GitHub Desktop.
jQueryを使ってテキストノードのみを抽出する。
// テキストノードのみを抽出して返す
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