Introduction to layui underlying methods

Release: 2019-12-21 17:29:35
forward
2723 people have browsed it

Introduction to layui underlying methods

Layui modular page usage:

Load modules as needed

//引入js,css文件跳过,看文档

//在页面中,如同编写JavaScript一样
Copy after login

Load all modules at once

Copy after login

Self-written module

//js文件
layui.define(['layer'],function(exports){//自写模块需要调用的layui模块
    var layer = layui.layer;//初始化模块变量
    
    layer.msg('Hi!');
    
    exports('index',{})//导出自写模块,index为导出名,use的时候名字要和导出一致
})

//use时注意需要设定模块所在目录
Copy after login

Local storage operation

layui.data('test',//test表名
    {//对象
        key:'user_name',//键值对
        value:'用户1'
    }
)

//删除
layui。data('test',{
    key:'user_name',//需要删除的键
    remove:true//删除
})

//查
var local = layui.data('test');
console.info(local.user_name);

//根据删除的方法,我暂时认为,如果需要插入多个键值对,需要使用对象数组,而删除多个键值对,则未知了,因为没实际操作,因为本地存储其实不怎么会用这个,接入后端后大多用session或者redis
Copy after login

Various underlying methods that may not be used in practice

layui.device()//获取设备信息
layui.cache()//获得缓存
layui.extend(options)//拓展别名,给一个模块设置别名
layui.each(obj,fn)//对象(array,object,dom)遍历,可取代for
layui.getStyle(node,name) //获得dom节点的style属性值,如document.body,'font-size'
layui.img(url,callback,error)//图片预加载,可设置加载失败显示默认图从而避免图片加载失败没图的尴尬
layui.router()//获得路由
layui.hint()//向控制台输出异常,目前只能error,layui.hint().error('error')
layui.stope(e)//阻止事件冒泡
layui.onevent(modName,events,callback)//自定义模块事件
layui.event(modName,events,params)//执行自定义事件
layui.factory(modName)//获取模块定义时的回调函数
Copy after login

For more layui knowledge, please pay attention to the layui usage tutorial column.

The above is the detailed content of Introduction to layui underlying methods. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!