pure.js必须与现有的页面节点一起使用吗?

时间:2012-05-15 11:11:37

标签: javascript templates pure-js

<html lang="en">
  <head>
    <meta charset="utf-8">
    <script type='text/javascript' src="js/jquery/jquery-min.js"></script>
    <script type='text/javascript' src="js/pure/pure.js"></script>
  </head>
  <body>
    <div class='result'>Test Page</div>

    <script type='text/javascript'>
      $(document).ready(function(){
        var p;
        p = $("<div><ul><li></li></ul></div>");
        directives = {"li": "error"};
        data = {"error": "name must be between 3 and 250 characters long"};
        p.render(data, directives);
        $(".result").after(p);
        });
    </script>
  </body>
</html>

以上代码不会将数据插入到p对象中。但是以下工作,

<html lang="en">
  <head>
    <meta charset="utf-8">
    <script type='text/javascript' src="js/jquery/jquery-min.js"></script>
    <script type='text/javascript' src="js/pure/pure.js"></script>
  </head>
  <body>
    <div class='result'>Test Page</div>

    <script type='text/javascript'>
      $(document).ready(function(){
        var p;
        p = $("<div><ul><li></li></ul></div>");
        $(".result").after(p);
        directives = {"li": "error"};
        data = {"error": "name must be between 3 and 250 characters long"};
        p.render(data, directives);
        });
    </script>
  </body>
</html>

似乎插入数据,jquery对象(这里是p)必须操纵现有的html标签?像后者一样不合理,我希望第一个代码插入数据,但是如何? 谢谢,:))

1 个答案:

答案 0 :(得分:1)

render始终返回一个节点。如果模板在DOM中,则它将被渲染节点替换。

你可以这样做:

p = p.render(data, directives);
$(".result").after(p);

$(".result").after( p.render(data, directives) );