/ Published in: jQuery

I can't help thinking that good use of jQuery & Javascript must use extensive comments
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
var grammar = { BracedText : new RegExp('\{.*\}') }; $(function() { $('td.kanji').each(function() { // get text is in curly-braces, abort if nothing var sentence = $(this).text(); var $bracedText = sentence.match(grammar.BracedText); if ($bracedText === null) return; var bracedText = $bracedText[0].substr(1,$bracedText[0].length-2); // replace curly-braced text with a span, which serves as an insertion point sentence = sentence.replace(grammar.BracedText,'<span></span>'); // find first link, clone it var link = $(this).closest('tr').find('a:first').clone(); // change the link text to what was in the curly braces link.text(bracedText); //insert the new html $(this).html(sentence).find('span').append(link); }); });
Comments
