Created
February 20, 2011 20:40
-
-
Save lerouxb/836285 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
<style> | |
#detect.child > p, | |
#detect.firstchild p:first-child, | |
#detect.lastchild p:last-child, | |
#detect.nthchild p:nth-child(2n+1), | |
#detect.sibling b + b {display: block; width: 111px;} | |
</style> | |
*/ | |
(function($) { | |
var tests = [ | |
{name: 'child', selector: '> p'}, | |
{name: 'firstchild', selector: '> p'}, | |
{name: 'lastchild', selector: '> p'}, | |
{name: 'nthchild', selector: '> p'}, | |
{name: 'sibling', selector: 'b + b'} | |
]; | |
$.fn.detect = function() { | |
this.each(function() { | |
var $element = $(this); | |
$element.append('<div id="detect"><p><b> </b><b> </b></p></div>'); | |
var $detect = $element.find('> #detect'); | |
for (var t in tests) { | |
var test = tests[t]; | |
var name = test.name; | |
var selector = test.selector; | |
// remove existing and add the new one | |
$detect.attr('class', name); | |
if ($detect.find(selector).width() == 111) { | |
$element.addClass(name); | |
} else { | |
$element.addClass('no-'+name); | |
} | |
} | |
$detect.remove(); | |
}); | |
}; | |
})(jQuery); |
This file contains 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
<script src="detect.js"></script> | |
<style> | |
#detect.child > p, | |
#detect.firstchild p:first-child, | |
#detect.lastchild p:last-child, | |
#detect.nthchild p:nth-child(2n+1), | |
#detect.sibling b + b {display: block; width: 111px;} | |
</style> | |
<script> | |
$(document).ready(function() { | |
var $body = $('body'); | |
$body.detect(); | |
var required = ['child', 'firstchild', 'lastchild', 'nthchild', 'sibling']; | |
var failed = false; | |
for (var r in required) { | |
var klass = required[r]; | |
if (!$body.hasClass(klass)) { | |
failed = true; | |
break; | |
} | |
} | |
if (failed) { | |
$('#requirements-failed').css('display', 'block'); | |
} else { | |
$('#requirements-succeeded').css('display', 'block'); | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment