跳到主要內容

jQuery 初學者教學(1)

最近剛碰jQuery想寫一些東西留以後自己可以細查,廢話不多說以下就是jQuery的教學,今天要教的是網站上的一個範例,大家如果想要看原文的東西可以點此

在開始使用之前大家必須要到jQuery的網站中下載jQuery的載入檔,一般都是建議下載current release version,請點此,當然下載之後就是要引入網頁中啦,程式碼如下


<!--"jquery-1.3.2.min.js 必須要指向自己網站資料夾的位置"-->
<script type="text/javascript" src="jquery-1.3.2.min.js">
</script>


其中前半部份在教授jQuery的過濾器,和他的分類器,如果像以前的JavaScript就必需要寫成這樣子的格式


<javascript>
document.getElementById("id_name").value;
// or
document.getElementByName("html_name").href;
</javascript>


但是如果在jQuery中使用這樣子的方式即可


<javascript>
$("id_name").dosomething();
//當然,這個方法是沒辦法實際使用在jQuery上面
</javascript>


前面當然有許多方法可以用,但是今天要介紹如何使用jQuery直接實做一個範例
首先將底下的檔名存成rate.php


<?php

define('STORE', 'ratings.dat');

//將檔案寫入
function put_contents($file,$content) {
$f = fopen($file,"w");
fwrite($f,$content);
fclose($f);
}

if(isset($_REQUEST["rating"])) {
$rating = $_REQUEST["rating"];
$storedRatings = unserialize(file_get_contents(STORE));
$storedRatings[] = $rating;
//serialize->將整個陣列、變數、參數等資料變成字串格式(string)存入檔案中
put_contents(STORE, serialize($storedRatings));
//四捨五入取整數,2表示小數後取兩位
$average = round(array_sum($storedRatings) / count($storedRatings), 2);
//計算陣列數目
$count = count($storedRatings);
//傳回xml格式內容
$xml = "<ratings><average>$average</average><count>$count</count></ratings>";
//標頭檔格式為xml
header('Content-type: text/xml');
echo $xml;
}

?>


再來就是產生一個我們要顯示的網頁名稱我們就稱他為index.html


<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
// 選擇id="rating",並且在內容中加入文字
$("#rating").append("Please rate: ");

//產生1.2.3.4.5的連結選項
for ( var i = 1; i <= 5; i++ )
$("#rating").append("<a href='#'>" + i + "</a> ");

// id="rating" 的子節點 連結<a></a>點下後的匿名函式
$("#rating a").click(function(e){
//將原本連結的功能去除掉
e.preventDefault();

//送出要求給rate.php, 傳送的變數名稱為rating
$.post("rate.php", {rating: $(this).html()},
function(xml)
{
//輸出結果到id="rating"當中
//.hmlt()表示將資料輸出到裡面
$("#rating").html(
"Thanks for rating, current average: " +
$("average", xml).text() +
", number of votes: " +
$("count", xml).text()
);
});
});
});
</script>
</head>
<body>
<!--網頁裡面只要加入rating就可以了-->
<div id="rating"></div>
</body>
</html>


好了,大家可以直接點一下index.html會發現有五個選項可以點,後來這些資料會儲存在資料夾中的ratings.dat這個檔案的內容會將rate.php裡面的$storedRatings陣列使用serialize()這個function變成文字格式儲存起來,而且index.html會顯示目前目前數目的總平均和點擊次數,這裡面完全都是使用jQuery儲存。

這只是一個範例,是我從jQuery擷取下來的,之後如果有更多閱讀心得,我會繼續PO到這個部落格上面。


留言

這個網誌中的熱門文章

[CSS] z-index 在不同瀏覽器繼承問題

今天會討論到這個課題,是因為要實做一個Popup dialog,所以我們希望的結果如下圖。 可是在IE7 卻發生了這樣的情況。 Popup不論怎麼設定z-index都無法浮在最上層,我們看一下html架構發生什麼事情。

[教學] 快快樂樂刪除CodeIgniter index.php

預設的CI網址預設都設定為index.php同一層級,因此所有的程式都必須指定index.php導向才能開始,例如 http://localhost/ci/index.php/welcome/test http://localhost/ci/welcome/test 本文將說明如何將惱人的index.php消除,還你一個漂亮的URL。 設定開始: 接下來說明如何使用rewrite方式將惱人的index.php去除。 rewrite不清楚的人,煩請先自行google 首先要先確定Apache的 mod_rewrite 有 開啟 ,如果沒有開啟請設定好之後重新啟動apache。 接著,在根目錄底下建立一個新檔案,檔名為 .htaccess ,裡面程式碼如下: <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] </IfModule> 接著到 application/config/config.php ,開啟檔案修改 $config['index_page'] = ""; 注意: /index.php/$1 要根據你目錄,例如 http://localhost/index.php ,網站根目錄為 /ci/index.php 則要寫成 /ci/index.php/$1 接著至CI目錄下,尋找 config\config.php , 修改一下裡面的檔案,修改如下: $config['index_page'] = ""; 存檔後,如此一來大功告成。 參考資料 官方網站說明

[教學] Mojito 安裝與入門,install mojito for beginner.

mojito ,最近終於從 YDN 對外公開此專案,這個套件主要用於解決前端多重裝置及瀏覽器端的問題,後端服務採取 node.js ,因此使用上必定要先準備以下幾個元素 準備素材 c++ complier git Node.js > 0.4.x NPM > 1.0.x 安裝方式 git clone git://github.com/yahoo/mojito.git cd mojito/source sudo npm install -g . npm install . 以上四個簡單的步驟,就可以把 mojito module 完整安裝到服務器上,接著就可以開始進入 mojito 的世界 使用方式 mojito 提供了完整的 command line 給開發者使用,接著先建立一個基本 hello world 專案,跟著以下步驟完成第一個專案。首先建立一個 mojito application, mojito create app hello cd hello 切換到目錄之後,再接著建立自己的 mojit,這邊的 mojit 就像是一個應用(application)可能會包含許多個獨立網站體,擁有獨立架構的 MVC ,包含內部設定等,詳細資料可以參考官方的 說明 ,建立 mojit mojito create mojit HelloMojit 輸入指令後,會看到顯示結果如下, creating mojit called 'HelloMojit' (using "default" archetype) ✔ mojit: HelloMojit created! ✔ mojito done. 接著修改 application.json 這個 mojit 設定檔案,讓剛才新建立的 hellp application 指定底下有一個 mojit -> HelloMojit ,讓應用可以去執行 mojit controller ,修改如下, [ { "settings": [ "master" ], "appPort": 8666, "specs": { ...