2012-04-17 116 views
2

的index.htmlrequireJS - 用AJAX請求

<link href="Styles/bootstrap.css" rel="stylesheet" /> 
<script data-main="Scripts/app" src="Scripts/require.js"></script> 

<a class="btn0">button 0</a> 
<div class="target"></div> 

app.js

require({ 
    paths : { 
     jQuery : 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min', 
     tooltip : 'bootstrap-tooltip' 
    } 
}); 

require([ 'jQuery' ], function() { 
    require(['tooltip' ], function() { 
     $(".btn0").click(function() { 
      $(".target").load('btn0.html'); 
     }); 

     $('[rel=tooltip]').tooltip(); 
    }); 
}); 

btn0.html

Wouldn't it be nice to see a Tooltip <a href="#" rel="tooltip" title="Oh yeah!">here</a>? 

在這種情況下,工具提示不起作用。只有工作,如果我切$('[rel=tooltip]').tooltip();並粘貼在btn0.html,如:

<script> 
$('[rel=tooltip]').tooltip(); 
</script> 

我的問題是。如何組織btn0.html中所需的JavaScript代碼?有可能把btn0.html的JS內容放在app.js中?

回答

5

您必須在加載btn0.html時調用tooltip()函數。 在初始化頁面時,rel="tooltip"不存在。

require([ 'jQuery' ], function() { 
    require(['tooltip' ], function() { 
    $(".btn0").click(function() { 
     $(".target").load('btn0.html', function() 
     { 
     $('[rel=tooltip]').tooltip(); 
     }) 
    }); 
    }); 
});