我是jquery的新手。我試圖在java中的HTML頁面追加Jquery。包括我寫的jquery.js文件下面的代碼:在HTML中動態追加jquery
scriptTag += "var script = document.createElement('script');" +
"script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'; " +
"script.type = 'text/javascript'; " +
"document.getElementsByTagName('head')[0].appendChild(script);" +
,然後我附加在與它
"var script2 = document.createElement('script'); window.onload = function() {" +
"$(document).ready(function() {" +
"$(\"#identity\").hide();});};" +
"script2.type = 'text/javascript'; " +
"document.getElementsByTagName('head')[0].appendChild(script2);";
所以基本上我想寫這個js + jQuery代碼:
var script = document.createElement('script');
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);
var script2 = document.createElement('script');
window.onload = function() {
$(document).ready(function() {
$("#identity").hide();
});
};
script2.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script2);
我想要做的是,我想我的功能窗口加載後。不知何故,單獨寫作$(document).ready(function() {
does not工作。我得到一個錯誤,未定義$(看起來像jquery.js還沒有準備好)。
爲避免此問題,我使用了window.onload = function() {
。但現在我得到錯誤:$(document).ready is not a function
。在這裏我很困惑如何寫這個東西。這是正確的方法嗎?任何幫助/指導,高度讚賞。
[編輯]
請注意,下面的代碼(不包括jQuery的)正常工作:
window.onload = function() {
document.getElementById('identity').style.visibility='hidden';
};
[編輯]
其實我想提出一個web代理,在那裏我下載頁面並以自定義的外觀和視野爲他們服務。這些頁面不包含任何jquery文件,也不能包含或編寫HTML。我只能加我的js動態地使用Java等
這不是腳本加載的一般工作方式;你需要在jQuery中查看['.getScript()'](http://api.jquery.com/jQuery.getScript/)並將其放入一個文件中以包含URL。在Javascript中添加腳本有點微妙,jQuery並沒有使它更容易。 –
'script2'爲空 – Alexander
@Alexander:不,它不是。你能用代碼指出嗎? –