2010-11-28 155 views
1

對不起,如果標題不太清楚,但我認爲它是正確的。不過,我想要做的有點像(在某種程度上)用JQuery(pref)構建一個小部件,PHP & CSS。在.js中包含jquery腳本頁面

我真的很希望發生的事情是我網站的「成員」只需在他們的HTML中粘貼2行代碼來加載小部件。事情是這樣的:

<script type="text/javascript" src="http://www.mydomain.com/script.js"></script> 

然後顯示控件像這樣<div id="displaywidget"></div>

確定該位是「容易」和確定。但我怎麼包括jQuery或「東西」來生成在script.js的小部件

我的意思是「displaywidget」 - 小部件div的ID將是我的服務器上的PHP文件的名稱,基本上是腳本.js將需要將displaywidget.php加載到div displaywidget中。

我想我使用document.getElementById('displaywidget')獲得div,但是如何在div中寫入/插入/加載displaywidget.php?

思考我寫的「純」java可以做「我想要的大部分,即document.getElementById('displaywidget'),但我更願意」包括「Jquery.js,因爲我希望小部件的某些方面使用JQuery。 JQuery UI日期函數

對不起,如果我漫不經心,但試圖思考,因爲我一直沿着我的「真正的」問題是我不太確定「純」的JavaScript即獲得div顯示/加載displaywidget.php

建議請(哦,如果我找錯了樹,請隨時告訴我 - 很好:))。

在此先感謝

回答

0

多一點 「拖網&認爲」 我發現這個代碼後:

(function() { 
// Localize jQuery variable 
var jQuery; 
/******** Load jQuery if not present *********/ 
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.4.2') { 
var script_tag = document.createElement('script'); 
script_tag.setAttribute("type","text/javascript"); 
script_tag.setAttribute("src","http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"); 
script_tag.onload = scriptLoadHandler; 
script_tag.onreadystatechange = function() { // Same thing but for IE 
if (this.readyState == 'complete' || this.readyState == 'loaded') { 
scriptLoadHandler(); 
} 
}; 
// Try to find the head, otherwise default to the documentElement 
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag); 
} else { 
// The jQuery version on the window is the one we want to use 
jQuery = window.jQuery; 
main(); 
} 
/******** Called once jQuery has loaded ******/ 
function scriptLoadHandler() { 
// Restore $ and window.jQuery to their previous values and store the 
// new jQuery in our local jQuery variable 
jQuery = window.jQuery.noConflict(true); 
// Call our main function 
main(); 
} 
/******** Our main function ********/ 
function main() { 
jQuery(document).ready(function($) { 
******* Load CSS *******/ 
var css_link = $("<link>", { 
rel: "stylesheet", 
type: "text/css", 
href: "style.css" 
}); 
css_link.appendTo('head');   

/******* Load HTML *******/ 
var jsonp_url = "http://al.smeuh.org/cgi-bin/webwidget_tutorial.py?callback=?"; 
$.getJSON(jsonp_url, function(data) { 
$('#example-widget-container').html("This data comes from another server: " + data.html); 
}); 
}); 
} 
})(); // We call our anonymous function immediately 

writtend由Alex Marandon,發現這裏http://alexmarandon.com/articles/web_widget_jquery/ - 作品,正是我想要的,包括/將JQuery安裝到.js文件中

0

我想我使用document.getElementById('displaywidget')來獲取div,但是如何在div中寫入/插入/加載displaywidget.php?

您正在尋找jQuery中的AJAX行爲,它會調用php頁面,然後將數據推送到div中。

你應該在這個過程的早期加載jQuery,就在你的head元素的前面。一旦它被加載,它將被緩存,所以不用擔心它在每個頁面上。沒有真正的開銷發生。

一旦安裝了jQuery,您可以調用與獲取數據和流行元素相關的許多AJAX函數之一。 Theres $ .load(),$ .ajax()和其他一些可以逃脫我的東西,除非我去看看他們的文檔部分。

你可以在沒有jQuery的情況下完成所有這些工作,但是它的代碼更多,你必須控制瀏覽器的差異。

+0

Dennis,thanks for但不太清楚如何將jquery「加載」到script.js中,因爲並非我的所有成員都在其域中安裝了Jquery。如果我能「摸清」如何在script.js中包含jquery,那麼此後應該是仙子(他希望) – rmap 2010-11-28 08:03:38

0

您可以將jquery加載到script.js中,只需將它複製並粘貼到script.js中的任何javascript之前或之後即可。

所以,如果腳本。JS是:

//start of file 
alert('ex'); 
//end of file 

讓它:

//start of file 
alert('ex') 
Copy and pasted Jquery source 
//end of file 
相關問題