/*** Criado pelo administrador em 15-1-19. */function functionUtil () {} functionUtil = {// faz um nó DOM tem um determinado atributo hasattr: function (el, nome) {var att = el.getAttributenode && el.getAttributenode (nome); retornar att? Att.Specificada: false}, // Obtenha o elemento de acordo com a classe getByclass: function (sclass, oparent) {oparent = oparent || documento; if (! Oparent.getElementsByClassName) {return oparent.getElementsByClassName (sclass); } var arr = []; var aele = oparent.getElementsByTagName ('*'); var reg = novo regexp ('(^| // s)' + sclass + '(// s | $)'); // var reg = novo regexp ('(^| [// x20 // t // r/n // f])' + sclass + '([// x20 // t // r/n // f] | $)'); for (var i = 0; i <aele.length; i ++) {if (reg.test (aele [i] .className)) {arr.push (aele [i]); }} retornar arr; }, // Adicione dinamicamente o Stylesheet AddSheetfile: function (path) {var fileref = document.createElement ("link") fileref.rel = "stylesheet"; fileref.type = "text/css"; fileref.href = caminho; fileref.media = "Screen"; var headobj = document.getElementsByTagName ('Head') [0]; Headobj.appendChild (fileref); }, // Ligue os dados JSON de acordo com o formato especificado, como $ {name} loadjsondata: function (sparent, ojson) {var oparent = document.getElementById (Sparent); if (instância ojson de matriz) {var str = oparent.innerhtml; for (var i = 0; i <ojson.length - 1; i ++) {oparent.innerhtml+= str; } para (var d em ojson) {oparent.children [d] .innerhtml = oparent.children [d] .innerhtml.replace (// $/{(/w+)/}/g, função (str, $ 1) {return ojson [d] [$ 1] ojson [d]; }} else {oparent.innerhtml = oparent.innerhtml.replace (// $/{(/w+)/}/g, function (str, $ 1) {return ojson [$ 1]? ojson [$ 1]: ';}); }}, // Ligue os dados JSON de acordo com o formato especificado como <%...%> modelEngine: function (html, opções) {html = html.replace (/(>) | (<)/g, function (str, $ 1, $ 2) {switch (str); var re =/<%([^%>]+)?%>/g, reexp =/(^()? (se | para | else | switch | case | break | {|})) (.*)?/g, code = 'var r = [];/n', cursor = 0; var add = function (linha, js) {js? (código + = line.match (reexp)? Linha + '/n': 'r.push (' + line + ');/n'): (code + = line! = ''? retornar add; } while (match = re.exec (html)) {add (html.slice (cursor, match.index)) (corresponde [1], true); cursor = match.index + corresponde [0] .Length; } add (html.substr (cursor, html.length - cursor)); código += 'return r.join ("");'; Retorne nova função (code.replace (/[/r/t/n]/g, '')). Aplicar (opções); }}1. O primeiro método: $ {key}
functionutil.loadjsondata (elemento, dados);
código "html":
<div id = "dados"> <div> Nome: $ {name} <br/> Idade: $ {Age} <br/> ocupação: $ {job} <br/> <br/> </div> </div>Código JavaScript:
var dados = [{nome: 'xu lei', idade: 24, trabalho: 'it'}, {name: 'li lei', idade: 23, trabalho: 'tradução'}]; functionUtil.loadjsondata ('dados', dados);Resultados da execução:
2. O segundo método < % code %>
functionUtil.TemplateEngine (String, Object);
código "html":
<div id = "test3"> <%if (this.isshow) {for (var i em this.data) {%> <p href = "#"> nome: <%this.data [i] .name%> </p> <p href = "#"> idade: <%this.data [i]. <%this.data [i] .job%> </p> <br/> <%}}%> </div>Código JavaScript:
var pessoa = {data: [{name: 'xu lei', idade: 24, trabalho: 'it'}, {name: 'li lei', idade: 23, trabalho: 'tradução'}], isShow: true} document.getElementById ("test3").resultado:
O acima é o conteúdo inteiro deste artigo. Você ganhou um novo entendimento dos modelos de JavaScript depois de lê -lo? Eu espero que você possa gostar.