Created
December 10, 2013 11:05
-
-
Save jirivrany/7888993 to your computer and use it in GitHub Desktop.
JQuery enhance links with class by file type
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title></title> | |
<style type="text/css"> | |
a { | |
color: orange; | |
} | |
.pdf { | |
color: red; | |
} | |
.doc { | |
color: green; | |
} | |
</style> | |
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> | |
</head> | |
<script type="text/javascript"> | |
$(document).ready( function() { | |
var endsWith = function endsWith(str, suffix) { | |
return str.indexOf(suffix, str.length - suffix.length) !== -1; | |
} | |
$("a").each( function(){ | |
if ( endsWith(this.href, ".pdf") ) { | |
$(this).addClass( "pdf" ); | |
} | |
else if ( endsWith(this.href, ".doc") ) { | |
$(this).addClass( "doc" ); | |
} | |
}); | |
} | |
); | |
</script> | |
<body> | |
<p> | |
<a href="ahoj.html">ahoj</a> | |
</p> | |
<p> | |
<a href="soubor.pdf">pdf file</a> | |
</p> | |
<p> | |
<a href="word.doc">se s tim smiř</a> | |
</p> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment