Home > Article > Web Front-end > Introduction to layui underlying methods
Layui modular page usage:
Load modules as needed
//引入js,css文件跳过,看文档 //在页面中,如同编写JavaScript一样 <script> layui.use(['layer','form'],function(){//需要加载的模块 var layer = layui.layer, form = layui.form;//初始化模块变量 layer.msg('Hi!');//调用该模块的方法 }) </script>
Load all modules at once
<script> ;!function(){ var layer = layui.layer ,form = layui.form;//初始化模块变量 layer.msg('Hello World'); }(); </script>
Self-written module
//js文件 layui.define(['layer'],function(exports){//自写模块需要调用的layui模块 var layer = layui.layer;//初始化模块变量 layer.msg('Hi!'); exports('index',{})//导出自写模块,index为导出名,use的时候名字要和导出一致 }) //use时注意需要设定模块所在目录 <script> layui.config({ base:'/res/js/modules/' //自建模块所在目录 }).use('index',function(){//加载入口 }); </script>
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
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)//获取模块定义时的回调函数
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!