【发布时间】:2011-09-18 11:26:43
【问题描述】:
我正在编写一个工具,它将基于 JSON 数据生成 HTML,并将输出放在文本区域中以便于复制/粘贴。我正在使用 jquery 模板来生成 HTML,但我无法将结果放入 textarea,因为 .tmpl() 返回一个 jQuery 对象(DOM 元素的集合)。
编辑:
用更简单的版本测试后发现 $("#HtmlPageTemplate").tmpl().html();确实有效。所以问题出在实际的模板上。关于使以下模板工作的任何建议?
<script id="HtmlPageTemplate" type="text/x-jquery-tmpl">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>API - ${ObjectName}</title>
<link rel="stylesheet" href="styles/meyer-reset.css" type="text/css" media="all" />
<link rel="stylesheet" href="scripts/google-code-prettify/src/prettify.css" type="text/css" media="all" />
<link rel="stylesheet" href="jquery-ui-1.8.13.custom/css/smoothness/jquery-ui-1.8.13.custom.css" type="text/css" media="all" />
<link rel="stylesheet" href="styles/api-documentation.css" type="text/css" media="all" />
<script src="jquery-ui-1.8.13.custom/js/jquery-1.5.1.min.js">{{html "</sc"+"ript>"}}
<script src="jquery-ui-1.8.13.custom/js/jquery-ui-1.8.13.custom.min.js">{{html "</sc"+"ript>"}}
<script src="scripts/google-code-prettify/src/prettify.js">{{html "</sc"+"ript>"}}
<script>
$(function(){
if ("onhashchange" in window) { // event supported?
window.onhashchange = function () {
SelectTabByAnchor(window.location.hash);
}
}
else { // event not supported:
var storedHash = window.location.hash;
window.setInterval(function () {
if (window.location.hash != storedHash) {
storedHash = window.location.hash;
SelectTabByAnchor(storedHash);
}
}, 100);
}
function SelectTabByAnchor(anchor){
var tab = -1;
if(anchor.substr(0, 10) == "#Property-"){
tab = 0;
}
else if(anchor.substr(0, 8) == "#Method-"){
tab = 1;
}
else if(anchor.substr(0, 7) == "#Event-"){
tab = 2;
}
if(tab > -1){
$("#PropertiesMethodsEventsContainer").tabs("select", tab);
//window.location.href = window.location.hash;
window.location.hash = window.location.hash;
}
}
$("a").click(function(event){
var anchor = $(this).attr("href");
SelectTabByAnchor(anchor);
// Stop the .Item event from showing/hiding the details
event.stopPropagation();
});
$(".Item").click(function(){
$(this).next().toggle();
}).hover(function(){
$(this).addClass("ui-state-hover");
},
function(){
$(this).removeClass("ui-state-hover");
});
prettyPrint();
$("#PropertiesMethodsEventsContainer").tabs();
if(window.location.hash){
//SelectTabByAnchor(window.location.hash);
$("a[href=\"" + window.location.hash + "\"]:first").trigger("click");
}
});
{{html "</sc"+"ript>"}}
</head>
<body>
{{tmpl "#ClassTemplate"}}
</body>
</html>
</script>
【问题讨论】:
标签: jquery string templates dom jquery-templates