2
<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标签?像后者不合理,我希望第一个代码插入数据,但是如何?谢谢, :)

4

1 回答 1

1

render总是返回一个节点。如果模板在 DOM 中,它会被渲染的节点替换。

你可以这样做:

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

或者

$(".result").after( p.render(data, directives) );
于 2012-05-16T06:59:14.153 回答