這篇文章將為大家詳細(xì)講解有關(guān)使用layui怎么定義一個(gè)模塊,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

先定義一個(gè)模塊
//定義模塊
layui.define(['form'], function(exports){
var form = layui.form; //只有執(zhí)行了這一步,部分表單元素才會(huì)自動(dòng)修飾成功
var $ = layui.$;
var obj = {
changeParentPlace: function (parentId,tagId,levelPlace) {
$.ajax({
url:ctx + '/base/place/changeParentPlace',
type:'get',
data:{
parentId:parentId
},
dataType:'json',
success:function (data) {
$("#" + tagId).empty();
$("#" + tagId).append('<option value="">請(qǐng)選擇</option>');
if(data.result != null && data.result.length != 0) {
$.each(data.result,function(i,place) {
if(levelPlace == place.name) {
$("#" + tagId).append('<option selected value="'+place.id+'">'+place.name+'</option>')
}else {
$("#" + tagId).append('<option value="'+place.id+'">'+place.name+'</option>')
}
})
}
form.render('select'); //刷新select選擇框渲染
}
})
}
}
//輸出模塊
exports('common', obj);
});定義方法使用layui.define
define中的第一個(gè)參數(shù)可以加載一些layui中內(nèi)置的模塊來(lái)使用,第二個(gè)回調(diào)方法中定義一些公共的方法
exports的第一參數(shù)是這個(gè)模塊起的名字,第二個(gè)參數(shù)是一個(gè)對(duì)象該對(duì)象中定義了這三個(gè)方法
設(shè)置模塊
//設(shè)置模塊
layui.config({
base: ctx + '/js/app/modules/' //假設(shè)這是你存放拓展模塊的根目錄
}).extend({ //設(shè)定模塊別名
common:'common'
});定義模塊使用layui.config
base定義的是定義模塊的js所在目錄
common是設(shè)置的別名,'common'是目錄中的文件名(.js自動(dòng)加)
//調(diào)用模塊
layui.use(['common'], function(){
var common = layui.common;
common.changeParentPlace($("#firstLevelId").val(),'secondLevelId');
});先將common模塊use出來(lái),調(diào)用代碼如上common.changeParentPlace($("#firstLevelId").val(),'secondLevelId');和使用內(nèi)置模塊是一樣的
關(guān)于使用layui怎么定義一個(gè)模塊就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)站題目:使用layui怎么定義一個(gè)模塊-創(chuàng)新互聯(lián)
標(biāo)題URL:http://www.chinadenli.net/article46/iohhg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站設(shè)計(jì)公司、全網(wǎng)營(yíng)銷推廣、靜態(tài)網(wǎng)站、定制網(wǎng)站、域名注冊(cè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容