JQuery省市聯(lián)動(dòng)效果實(shí)現(xiàn)過(guò)程詳解
Js相關(guān)技術(shù)
JS中的數(shù)組: ["城市"]
new Array()
DOM樹(shù)操作:
- 創(chuàng)建節(jié)點(diǎn): document.createElement
- 創(chuàng)建文本節(jié)點(diǎn): document.createTextNode
- 添加節(jié)點(diǎn): appendChild
需求分析
在我們的注冊(cè)表單中,通常我們需要知道用戶的籍貫,需要一個(gè)給用選擇的項(xiàng),當(dāng)用戶選中了省份之后,列出省下面所有的城市
技術(shù)分析
準(zhǔn)備工作 : 城市信息的數(shù)據(jù)
添加節(jié)點(diǎn) : appendChild (JS)
a. append : 添加子元素到末尾
$("#div1").append("<font color='red'>this is replacing text</font>")
b. appendTo : 給自己找一個(gè)爹,將自己添加到別人家里
$("#div1").prepend("<font color='red'>this is replacing text</font>")
和第一個(gè)效果一樣
c. prepend : 在子元素前面添加
$("#div1").after("<font color='red'>this is replacing text</font>")
d. after : 在自己的后面添加一個(gè)兄弟
$("<font color='red'>this is replacing text</font>").appendTo("#div1")

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="js/jquery-1.11.0.js"></script>
<script>
$(function () {
$("#btn1").click(function () {
// $("#div1").append("<font color='red'>this is replacing text</font>")
// $("#div1").prepend("<font color='red'>this is replacing text</font>")
$("#div1").after("<font color='red'>this is replacing text</font>")
// $("<font color='red'>this is replacing text</font>").appendTo("#div1")
});
});
</script>
</head>
<body>
<input type="button" value="click me, replace text" id="btn1">
<div id="div1">this is a text that will be replaced!</div>
</body>
</html>
遍歷的操作:
<script>
var cities = ["深圳市", "東莞市", "惠州市", "廣州市"];
$(cities).each(function (i, n) {
console.log(i + "====" + n);
})
$.each(cities, function (i, n) {
console.log(i + ">>>>" + n);
})
</script>

步驟分析:
- 導(dǎo)入JQ的文件
- 文檔加載事件:頁(yè)面初始化
- 進(jìn)一步確定事件: change事件
- 函數(shù): 得到當(dāng)前選中省份
- 得到城市, 遍歷城市數(shù)據(jù)
- 將遍歷出來(lái)的城市添加到城市的select中
代碼實(shí)現(xiàn):
$(function(){
$("#province").change(function(){
// alert(this.value);
//得到城市信息
var cities = provinces[this.value];
//清空城市select中的option
/*var $city = $("#city");
//將JQ對(duì)象轉(zhuǎn)成JS對(duì)象
var citySelect = $city.get(0)
citySelect.options.length = 0;*/
$("#city").empty(); //采用JQ的方式清空
//遍歷城市數(shù)據(jù)
$(cities).each(function(i,n){
$("#city").append("<option>"+n+"</option>");
});
});
});
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- jQuery-Citys省市區(qū)三級(jí)菜單聯(lián)動(dòng)插件使用詳解
- jquery+php后臺(tái)實(shí)現(xiàn)省市區(qū)聯(lián)動(dòng)功能示例
- 使用PHP+MySql+Ajax+jQuery實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)功能示例
- jquery+ajax實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)(封裝和不封裝兩種方式)
- jQuery ajax實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)
- jQuery實(shí)現(xiàn)的省市聯(lián)動(dòng)菜單功能示例【測(cè)試可用】
- 省市區(qū)三級(jí)聯(lián)動(dòng)jquery實(shí)現(xiàn)代碼
- jQuery+jsp實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)效果(附源碼)
相關(guān)文章
jquery 輸入框查找關(guān)鍵字并提亮顏色的實(shí)例代碼
下面小編就為大家分享一篇jquery 輸入框查找關(guān)鍵字并提亮顏色的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Expandable "Detail" Table Rows
Expandable "Detail" Table Rows...2007-08-08
jQuery實(shí)現(xiàn)獲取table表格第一列值的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)獲取table表格第一列值的方法,涉及jQuery針對(duì)table表格元素的遍歷與讀取相關(guān)技巧,需要的朋友可以參考下2016-03-03
基于JQuery的列表拖動(dòng)排序?qū)崿F(xiàn)代碼
基于JQuery的拖動(dòng)插件有幾個(gè)都相當(dāng)好用,效果也很好,但再好,還是自己琢磨一個(gè)最好。所以,我的理念就是即使實(shí)際項(xiàng)目中使用別人的程序,自己也得根據(jù)自己的理解和想法寫(xiě)一個(gè)出來(lái)。那么今天,就來(lái)看看我的思路是不是可以實(shí)現(xiàn)拖動(dòng)排序的功能2013-10-10
jQuery實(shí)現(xiàn)回車鍵(Enter)切換文本框焦點(diǎn)的代碼實(shí)例
這篇文章主要介紹了jQuery實(shí)現(xiàn)回車鍵(Enter)切換文本框焦點(diǎn)的代碼實(shí)例,需要的朋友可以參考下2014-05-05

