使用jquery创建某些html内容的新实例

Create new instance of certain html contentusing jquery

本文关键字:新实例 实例 创建 jquery html 使用      更新时间:2023-09-26

我正在尝试创建一个模式框,从启动加载某些html内容

比方说我有一个链接

<a href='#'>LINK</a>

现在,我有了要使用模式加载的内容,该模式是隐藏的,当点击上的链接时加载

<div id='modal'>
// more content goes here
</div>

现在我有了jQuery,当点击链接时,它会添加更多内容

$('#modal').live('click',function(){
// more appending done here
});

上面的工作原理很好,只有一个链接在使用中,但有多个链接像这个

 <a href='#'>LINK</a>
 <a href='#'>LINK</a>
 <a href='#'>LINK</a>

如果我点击它们中的每一个,它们每次都会单独附加相同的内容,现在我知道我可以在每次使用开始时使用.empty(),但我想知道如何使用<script type = "text/template"> // content </script>或任何其他创建实例的方法来实现这一点

谢谢各位

$("body").on("click","a",function(){
    var modal = $("<div id="modal"></div>");
    modal.html(<content goes here>);
    return false;
})

因此,每次单击标记时,都会在模态变量中得到一个新对象。