First on the code
var formatString = function (str,data) {return str.replace (/\{# (\w+) #\}/g,function (Match,key) {return typeof Data[key] = = = undefined? ": Data[key]});} Self.each (function () {_this = $ (this);//_html = ' <div class= ' ' +iset.iselectbox+ ' "><div class=" ' + iset.iselectcurrent+ ' style= ' width: ' +iset.width+ ' px ' > ' +self.find (' option:selected '). Text () + ' </div> <dl class= "' +iset.iselectoption+ '" style= "display:none; z-index:9999; Width: ' +iset.width+ ' px; > ';//template mode Startvar TPL = {d: [' <div class= ' {#iselectbox #} ' > ', ' <div class= ' {#iselectcurrent #} ' style= ' width : {#width #}px ' > ', ' {#text #} ', ' </div> ', ' <dl class= ' {#iselectoption #} ' style= ' Display:none; z-index:9999; width:{#width #}px; " > '].join (')}var idata = {iselectbox:iset.iselectbox,iselectcurrent:iset.iselectcurrent,width:iset.width,text: Self.find (' option:selected '). Text (), Iselectoption:iset.iselectoption}console.log (idata); _html + = FormatString ( tpl[' d '],idata);//Template mode end
Mainly used in the application of string splicing, this can reduce the previous frequent splicing errors, the format is more clear.
How to apply a template pattern in existing code