使用小胡子填写html标签参数 [英] Using mustache to fill in a html tag argument

查看:116
本文介绍了使用小胡子填写html标签参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用胡须在下面的href中附加一个标识符(id)

I'm trying the append an identier (id) to the href below using mustache

模板:

<div id='tmpl'>
    <a href='#product_detail?'{{id}}>link</a>
</div>    

var template = $('#tmpl').html();

数据:

var model = { id: 22 };

使用模型数据渲染模板:

Rendering the template using the model data:

var html = Mustache.to_html(template, model);

导致:

<a href="#product_detail?%7B%7Bid%7D%7D">link</a>

如果模板更改为:

<div id='tmpl'>
    <a href='#product_detail?'{{id}}>link</a>
</div>

生成的模板为:

<a href="#product_detail?" 0="">link</a>

问题"似乎是jQuery将模板中的单引号更改为使胡须混淆的双引号.将小胡子标记放在引号之外也不会产生正确的结果.该如何解决?

The 'problem' seems to be that jQuery is changing the single quotes in the template to double quotes that confuses mustache. Placing the mustache tag outside the quotes doesn't give the right results either. How can this be solved?

谢谢

推荐答案

您需要在href中添加{{id}},如下所示:

You need to add the {{id}} in the href, like that :

href="#product_detail?{{id}}"

我们过去通常将胡须模板放在类型为"text/template"的脚本中,我不知道它是否可以解决您的转义问题,但我们不在这里.

We used to put our mustache template in a script with type="text/template", i don't know if it will solve your escaping problems but we haven't here.

<script type="text/template" id="tmpl">
   <a href href="#product_detail?{{id}}">link</a>
</script>

如果遇到一些转义问题,请使用&在这样的ID之前:

If you got some escaping problems use the & before the id like that:

<script type="text/template" id="tmpl">
   <a href href="#product_detail?{{& id}}">link</a>
</script>

希望对您有所帮助!

这篇关于使用小胡子填写html标签参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆