2012-05-15 53 views
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 object.But以下工作,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>"); 
     $(".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

render總是返回一個節點。如果模板位於DOM中,則將其替換爲渲染的節點。

你可以這樣做:

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

$(".result").after(p.render(data, directives)); 
相關問題