【发布时间】:2016-09-03 06:07:28
【问题描述】:
我正在尝试使用 jQuery 插入一些模板,并在使用时得到两个不同的结果:
一)
var $template = $("#productTemplate").html();
b)
var $template = $($("#productTemplate").html());
如果我使用 a) case 我可以多次添加模板,如果我使用 b) 我只能添加一次模板。 那么有什么区别呢?
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="style.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="main.js"></script>
</head>
<body>
<div class="but">
<a href="#" class="showForm"> Click </a>
</div>
<script id='productTemplate' type='text/template'>
<div class="product">
<h1>H1</h1>
</div>
</script>
</body>
</html>
main.js
$(document).ready(function(){
var $template = $($("#productTemplate").html());
$(".showForm").on("click",function() {
$("body").append($template);
});
});
【问题讨论】:
标签: javascript jquery html append