jQuery function的正確書寫方法
更新時間:2013年08月02日 18:23:20 作者:
jQuery function想必大家都不會陌生,web前端開發(fā)人員經(jīng)常會接觸到,下面為大家介紹下它的正確書寫方法,感興趣的朋友可以參考下
jQuery 正確的書寫格式:
引入:<script src="/libs/jquery/jquery.js" type="text/javascript"></script>
html寫法:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="modules-js/qiyi.js"></script>
<script>
$(function() {
qiyiPlugin.init($("#zong_qiyi"));
});
</script>
</head>
<div id="zong_qiyi">
//XXXXXXXXXXXXXX
</div>
jQuery寫法:
var qiyiPlugin =
{
init: function(parentNode) {
var self = this;
self.parentNode = parentNode;
//刷新局部內(nèi)容
self.parentNode
.on('refresh',function(event){
self.qiyiList();
});
},
//顯示
qiyiList: function(){
var self = this;
//顯示內(nèi)功信息
self.getQiyiList(function(result){
//console.log(result);
if(result.qiyiList){
self.qiyiHtml1();//重新覆蓋html,防止重復(fù)顯示內(nèi)容
self.displayQiyiList(result.qiyiList);//展示頁面內(nèi)容
}else{
alert("沒有獲得奇藝數(shù)據(jù),請查看接口");
}
});
},
//顯示奇藝內(nèi)容
displayQiyiList: function(qiyiList){
var self = this;
var top = "#zong_qiyi";
$(top).find('#qiyi_control_panel').each(function(){
if ($(this).data('data')) $(this).remove();
});
var line0 = $(top).find("#qiyi_control_panel");
$.each(qiyiList,function(index,item2){
var line1 = $(line0).clone();
$(line1).data('data',item2);//附加屬性data與信息
$(line1).attr('id',index+1);
$(line1).find("#name").html(item2.name);
$(line1).find("#level").html(item2.level);
$(line1).find("#maxLevel").html(item2.maxLevel);
$(line1).find("#exp").html(item2.exp+"/"+item2.needExp);
$(line1).find("#desc1").html(item2.desc1);
$(line0).before(line1);
$(line1).show();
$(line1).find("#upgrade")
.on('click',function(event){
var table = $(this).parents('table:first');
var info = $(table).data('data');
self.clickToUpgradeQiyi(info, function(result){
if(!result.success){
alert(result.fail.desc1);
}else{
//alert(result.success.desc1);
growingPlugin.playerTopData();//改變attrs屬性
self.parentNode.trigger('refresh');
}
});
});
});
},
//賦html值-
qiyiHtml1: function() {
var string = "<table id='qiyi_control_panel' class='table table-bordered table-hover' style='display:none; width:500;'>";
string += "<tr><td>名稱:</td><td id=name>haoren_內(nèi)功</td></tr>";
string += "<tr><td>當(dāng)前等級:</td><td id=level></td></tr>";
string += "<tr><td>最大等級:</td><td id=maxLevel></td></tr>";
string += "<tr><td>exp:</td><td id=exp></td></tr>";
string += "<tr><td>描述:</td><td id=desc1></td></tr>";
string += "<tr><td><button id='upgrade' class='btn btn-link'>升級</button></td></tr>";
string += "</table>";
$("#zong_qiyi").html(string);
},
//奇藝升級
clickToUpgradeQiyi: function(info,callback) {
var query = "operation=upgradeQiyi";
query += "&qiyiName=" + info.name;
toolsPlugin.play(query,callback);
},
//獲得奇藝列表
getQiyiList: function(callback) {
var query = "operation=getQiyiList";
toolsPlugin.play(query,callback);
},
};
引入:<script src="/libs/jquery/jquery.js" type="text/javascript"></script>
html寫法:
復(fù)制代碼 代碼如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="modules-js/qiyi.js"></script>
<script>
$(function() {
qiyiPlugin.init($("#zong_qiyi"));
});
</script>
</head>
<div id="zong_qiyi">
//XXXXXXXXXXXXXX
</div>
jQuery寫法:
var qiyiPlugin =
{
init: function(parentNode) {
var self = this;
self.parentNode = parentNode;
//刷新局部內(nèi)容
self.parentNode
.on('refresh',function(event){
self.qiyiList();
});
},
//顯示
qiyiList: function(){
var self = this;
//顯示內(nèi)功信息
self.getQiyiList(function(result){
//console.log(result);
if(result.qiyiList){
self.qiyiHtml1();//重新覆蓋html,防止重復(fù)顯示內(nèi)容
self.displayQiyiList(result.qiyiList);//展示頁面內(nèi)容
}else{
alert("沒有獲得奇藝數(shù)據(jù),請查看接口");
}
});
},
//顯示奇藝內(nèi)容
displayQiyiList: function(qiyiList){
var self = this;
var top = "#zong_qiyi";
$(top).find('#qiyi_control_panel').each(function(){
if ($(this).data('data')) $(this).remove();
});
var line0 = $(top).find("#qiyi_control_panel");
$.each(qiyiList,function(index,item2){
var line1 = $(line0).clone();
$(line1).data('data',item2);//附加屬性data與信息
$(line1).attr('id',index+1);
$(line1).find("#name").html(item2.name);
$(line1).find("#level").html(item2.level);
$(line1).find("#maxLevel").html(item2.maxLevel);
$(line1).find("#exp").html(item2.exp+"/"+item2.needExp);
$(line1).find("#desc1").html(item2.desc1);
$(line0).before(line1);
$(line1).show();
$(line1).find("#upgrade")
.on('click',function(event){
var table = $(this).parents('table:first');
var info = $(table).data('data');
self.clickToUpgradeQiyi(info, function(result){
if(!result.success){
alert(result.fail.desc1);
}else{
//alert(result.success.desc1);
growingPlugin.playerTopData();//改變attrs屬性
self.parentNode.trigger('refresh');
}
});
});
});
},
//賦html值-
qiyiHtml1: function() {
var string = "<table id='qiyi_control_panel' class='table table-bordered table-hover' style='display:none; width:500;'>";
string += "<tr><td>名稱:</td><td id=name>haoren_內(nèi)功</td></tr>";
string += "<tr><td>當(dāng)前等級:</td><td id=level></td></tr>";
string += "<tr><td>最大等級:</td><td id=maxLevel></td></tr>";
string += "<tr><td>exp:</td><td id=exp></td></tr>";
string += "<tr><td>描述:</td><td id=desc1></td></tr>";
string += "<tr><td><button id='upgrade' class='btn btn-link'>升級</button></td></tr>";
string += "</table>";
$("#zong_qiyi").html(string);
},
//奇藝升級
clickToUpgradeQiyi: function(info,callback) {
var query = "operation=upgradeQiyi";
query += "&qiyiName=" + info.name;
toolsPlugin.play(query,callback);
},
//獲得奇藝列表
getQiyiList: function(callback) {
var query = "operation=getQiyiList";
toolsPlugin.play(query,callback);
},
};
您可能感興趣的文章:
- jQuery中isFunction方法的BUG修復(fù)
- (function($){...})(jQuery)的意思
- 解決jquery submit()提交表單提示:f[s] is not a function
- jQuery中(function(){})()執(zhí)行順序的理解
- Jquery中"$(document).ready(function(){ })"函數(shù)的使用詳解
- 淺析jQuery(function(){})與(function(){})(jQuery)之間的區(qū)別
- jQuery中;function($,undefined) 前面的分號的用處
- 簡單談?wù)刯Query(function(){})與(function(){})(jQuery)
相關(guān)文章
JQuery Ajax跨域調(diào)用和非跨域調(diào)用問題實例分析
這篇文章主要介紹了JQuery Ajax跨域調(diào)用和非跨域調(diào)用問題,結(jié)合具體實例形式分析了jQuery基于ajax的非跨域請求及跨域請求相關(guān)實現(xiàn)技巧與操作注意事項,需要的朋友可以參考下2019-04-04
Jquery對新插入的節(jié)點 綁定Click事件失效的解決方法
下面小編就為大家?guī)硪黄狫query對新插入的節(jié)點 綁定Click事件失效的解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06
jQuery方法簡潔實現(xiàn)隔行換色及toggleClass的使用
隔行換色的展示效果想必大家早已熟悉了吧,今天在幫大家回憶一下本例中主要用到的方法是toggleClass(),感興趣的你可不要錯過了哈,希望可以幫助到你2013-03-03
checkbox 選中一個另一個checkbox也會選中的實現(xiàn)代碼
本文給大家?guī)砹薱heckbox選擇器之checkbox 選中一個另一個checkbox也會選中的實現(xiàn)代碼 ,非常不錯,有需要的可以參考下2016-07-07

