javascript - Using jquery to prepare jQuery UI tabs, optimize? -
codereview link: https://codereview.stackexchange.com/q/26006/24914
i looking review solution jquery dom manipulation exercise on appendto.com: http://learn.appendto.com/lesson/dom-manipulation-101#exercise
i'm wondering if there way can optimize or if there other aspects need consider when building solution this.
the problem outlined in fiddle: http://jsfiddle.net/khwzs/1283/
here solution:
var getfirsttwowords = function(str) { return str.split(/\s+/).slice(1,3).join(" "); } var tablinks = $("<ul></ul>"); $("div.tab").each( function( idx ) { var id = "tabs-" + (idx + 1); var text = $(this) .removeclass("tab") .attr("id", id) .text(); var tablabel = getfirsttwowords(text); // create corresponding tab link tablinks.append("<li><a href=\"#" + id + "\">" + tablabel + "</a></li>"); }); $(".tabs div:first").before(tablinks); $( ".tabs" ).tabs();
optimized version:
var tablinks = $("<ul></ul>"); $("div.tab").each( function( idx ) { var id = "tabs-" + (idx + 1); var tablabel = $(this) .removeclass("tab") .attr("id", id) .find("h3") .remove() .text(); // create corresponding tab link tablinks.append("<li><a href=\"#" + id + "\">" + tablabel + "</a></li>"); }); $(".tabs") .prepend(tablinks) .tabs();
Comments
Post a Comment