问题
I have implemented basic template parser for javascript. it simply replaces variables within template string. i.e {event.date} will be 7/4/2013
I am using script tag to store template string
<script id="date_template" type="text/html"> <span class="date" id="date_{event.id}"> {event.date} <span> </script>
but it gives error in mobile devices so I have used div element for this
<div style="display:none" id="date_template"> <span class="date" id="date_{event.id}"> {event.date} <span> </div>
but it creates dom element which causes other problems. Is there any other way to do this?
回答1:
Here is another way to do this:
$("<span/>",
{
"class":"date",
"id":"date_$event.id".replace(/\$event.id/,event.id),
"html":event.date
}
)
References
- jquery constructor: jquery(html, attributes)
来源:https://stackoverflow.com/questions/19241096/javascript-template-parsing-in-mobile-devices