layui.laytpl如何從外部引入模板文件


首頁 index.html
<body>
    <div id="hello"></div>
    <script src="./lib/layui/layui.js"></script>
    <script>
        layui.use('layer', function(){ 
            var $ = layui.$;
            // 加載hello組件(會自動綁定組件對應的dom上)
            $("#hello").load("./components/hello.html");
        });
    </script>
</body>
組件 hello.html
<script id="demo" type="text/html">
    <h3>{{ d.title }}</h3>
</script>
<script>
    var data = { //數據
        "title": "helloword"
    }
    var getTpl = demo.innerHTML
        , view = document.getElementById('hello');

    layui.use('laytpl', function () {
        var laytpl = layui.laytpl;
        laytpl(getTpl).render(data, function (html) {
            view.innerHTML = html;
        });
    })
</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM