Skip to content

Instantly share code, notes, and snippets.

@sullenfish
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save sullenfish/9700458 to your computer and use it in GitHub Desktop.

Select an option

Save sullenfish/9700458 to your computer and use it in GitHub Desktop.
jQuery :only-text Pseudo-Class
jQuery.expr[":"]["only-text"] = function (elem) {
var i = elem.childNodes.length - 1, flag = false;
for (; i >= 0; i -= 1){
switch (elem.childNodes[i].nodeType){
case 3: // 3: TEXT_NODE
flag = true;
break;
case 7: // 7: PROCESSING_INSTRUCTION_NODE
case 8: // 8: COMMENT_NODE
break;
default:
return false;
}
}
return flag;
};
// Example:
jQuery('li:only-text').css('background-color', 'yellow');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment