【问题标题】:JQuery Templates: pass html parameter into a functionJQuery模板:将html参数传递给函数
【发布时间】:2016-05-17 17:38:27
【问题描述】:

我有这个 JQuery 模板:

$.tmpl('<p>${text}</p>', { text: 'cool text' }).appendTo('div');

效果很好。但是现在我想将一个html参数传递给它,但是以下不起作用。

$.tmpl('<p>${text}</p>', { text: '<span>cool text</span>' }).appendTo('div');

有什么建议吗?

【问题讨论】:

  • 使用的jquery模板引擎

标签: jquery html parameters jquery-templates


【解决方案1】:

您可以使用{{html content}} 构造

$.tmpl('<p>{{html text}}</p>', {
  text: '<span>cool text <b>bold</b></span>'
}).appendTo('div');
span {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.js"></script>
<div></div>

【讨论】:

    【解决方案2】:

    还有一种方法可以做到这一点

    var markup = "<span>${text}</span>";
    
    jQuery.template( "template1", markup );
    jQuery.tmpl( "template1", { text: "cool text" } ).appendTo( "div" );
    

    【讨论】:

      猜你喜欢
      • 2011-08-26
      • 2018-09-28
      • 1970-01-01
      • 2013-11-17
      • 1970-01-01
      • 2013-01-27
      • 2011-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多