How to use jquery in layui: 1. Actively load the jquery module with statements such as "layui.use(['jquery', 'layer']...)"; 2. Through "layui.use(' layer', function(){...}" method.
Recommended "layUI Tutorial"
layui uses internal jQuery:
Encountered problem situations:
Because some of Layui’s built-in modules depend on jQuery, jQuery is not introduced separately, but when using $When using the conventional writing method to obtain the dom element, it prompts that it is undefined
Cause of the problem:
Because some of the built-in modules of Layui depend on jQuery, we use the most stable version of jQuery1.11 as a built-in DOM module (the only third-party module). It will be loaded only if the module you use depends on it, and if your page has already introduced jquery via script, it will not be loaded repeatedly. The built-in jquery module Removed the global $ and jQuery, it is a standard module that conforms to the layui specification.
Two solutions:
//第一种:主动加载jquery模块 layui.use(['jquery', 'layer'], function(){ var $ = layui.$ //重点处 ,layer = layui.layer; //后面就跟你平时使用jQuery一样 $('body').append('hello jquery'); }); //第二种:如果内置的模块本身是依赖jquery,你无需去use jquery,所以上面的写法其实可以是: layui.use('layer', function(){ var $ = layui.$ //由于layer弹层依赖jQuery,所以可以直接得到 ,layer = layui.layer; //…… });
The above is the detailed content of How to use jquery in layui. For more information, please follow other related articles on the PHP Chinese website!