Posted By

PandaWood on 01/23/10


Tagged

jquery comments


Versions (?)

Can Good jQuery/Javascript not have lots of comments?


 / Published in: jQuery
 

I can't help thinking that good use of jQuery & Javascript must use extensive comments

  1. var grammar = {
  2. BracedText : new RegExp('\{.*\}')
  3. };
  4.  
  5. $(function() {
  6. $('td.kanji').each(function() {
  7.  
  8. // get text is in curly-braces, abort if nothing
  9. var sentence = $(this).text();
  10. var $bracedText = sentence.match(grammar.BracedText);
  11. if ($bracedText === null) return;
  12. var bracedText = $bracedText[0].substr(1,$bracedText[0].length-2);
  13.  
  14. // replace curly-braced text with a span, which serves as an insertion point
  15. sentence = sentence.replace(grammar.BracedText,'<span></span>');
  16.  
  17. // find first link, clone it
  18. var link = $(this).closest('tr').find('a:first').clone();
  19.  
  20. // change the link text to what was in the curly braces
  21. link.text(bracedText);
  22.  
  23. //insert the new html
  24. $(this).html(sentence).find('span').append(link);
  25. });
  26. });

Report this snippet  

You need to login to post a comment.