我已經使用了本教程使用下劃線的模板。 http://backbonetutorials.com/what-is-a-view/使用下劃線更新元素
我有一種情況,我只是想在p標籤中添加一些文本到div結構。 這不是一個模板,只是一些需要注入一些值的文本。 有沒有辦法使用下劃線來更新本文中的變量。或者,我需要創建文本作爲模板,然後添加使用HTML模板(_template)
<div id="popup">
<p>I want to add some text in here <%=variable1%>, Then add another variable text in here <%=variable2%></p>
</div>
UPDATE:
關閉它基於使用此代碼模板文檔我試圖做。
<html>
<head>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/underscore.js"></script>
<script type="text/javascript" src="js/core.js"></script>
</head>
<body>
<div id="popup">
<div id="template_holder"></div>
</div>
<!-- TEMPLATES FOR CANCEL PAYMENT MODEL -->
<script type="text/template" id="card-success_tmp">
<p>I want to add some text in here <%=variable1%>, Then add another variable text in here <%=variable2%></p>
</script>
</body>
</html>
然後在我的核心js文件我有下面這段代碼,會啓動我的彈出
var variables = {variable1:'[xxxx]', variable2:'[MM/YY]'};
var template = _.template($("#card-success_tmp").html(), variables);
$('#popup #template_holder').html(template);
click事件裏面,但上面仍然沒有工作。
我甚至試過
var template = _.template($("#card-success_tmp").html());
$('#popup #template_holder').html(template({variable1:'[xxxx]', variable2:'[MM/YY]'}));
它呈現的文本,但該變量傳遞沒有得到呈現。
如果我將模板作爲字符串添加而不是從腳本標記中添加,那麼它將起作用。 問題是爲什麼它不是從腳本標記工作。它呈現文本而不是傳遞給它的變量。
var template = _.template("<p>I want to add some text in here <%=variable1%>, Then add another variable text in here <%=variable2%></p>");
$('#popup #template_holder').html(template({variable1:'[xxxx]', variable2:'[MM/YY]'}));
普通下劃線,還是你有骨幹(包括jQuery)? – Bergi
普通下劃線沒有骨幹 – Chapsterj
但是你有jQuery? – Bergi