type參數 0(信息框,默認)1(頁面層)2(iframe層)3(加載層)4(tips層)
// 1.頁面層
layer.open({
type: 1,
content: '傳入任意的文本或html' //這裏content是一個普通的String
});
layer.open({
type: 1,
content: $('#id') //這裏content是一個DOM,注意:最好該元素要存放在body最外層,否則可能被其它的相對元素所影響
});
//Ajax獲取
$.post('url', {}, function(str){
layer.open({
type: 1,
content: str //注意,如果str是object,那麼需要字符拼接。
});
});
//2.iframe層
layer.open({
type: 2,
content: 'http://sentsin.com' //這裏content是一個URL,如果你不想讓iframe出現滾動條,你還可以content: ['http://sentsin.com', 'no']
});
// 3.tips層
layer.open({
type: 4,
content: ['內容', '#id'] //數組第二項即吸附元素選擇器或者DOM
});
btn按鈕回調
// 諮詢框
layer.confirm('納尼?', {
btn: ['按鈕一', '按鈕二', '按鈕三'], //可以無限個按鈕
btn3: function(index, layero){
//按鈕【按鈕三】的回調
}
}, function(index, layero){
//按鈕【按鈕一】的回調
}, function(index){
//按鈕【按鈕二】的回調
});
// 多個按鈕回調
layer.open({
content: 'test'
,btn: ['按鈕一', '按鈕二', '按鈕三']
,yes: function(index, layero){
//按鈕【按鈕一】的回調
}
,btn2: function(index, layero){
//按鈕【按鈕二】的回調
//return false 開啓該代碼可禁止點擊該按鈕關閉
}
,btn3: function(index, layero){
//按鈕【按鈕三】的回調
//return false 開啓該代碼可禁止點擊該按鈕關閉
}
,cancel: function(){
//右上角關閉回調
//return false 開啓該代碼可禁止點擊該按鈕關閉
}
});
確定yes和關閉cancel
layer.open({
content: '測試回調',
yes: function(index, layero){
//do something
layer.close(index); //如果設定了yes回調,需進行手工關閉
},
cancel: function(index, layero){
if(confirm('確定要關閉麼')){ //只有當點擊confirm框的確定時,該層纔會關閉
layer.close(index)
}
return false;
}
});