append
or prepend
method to Add elements to the page. Here is an example of using jQuery to add elements in Layui: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>使用jQuery添加元素</title> <link rel="stylesheet" href="layui/css/layui.css"> <script src="jquery.min.js"></script> <script src="layui/layui.js"></script> </head> <body> <div class="layui-container"> <button class="layui-btn" id="addBtn">添加元素</button> <div id="content"></div> </div> <script> layui.use(['jquery', 'layer'], function () { var $ = layui.$; $('#addBtn').on('click', function () { $('#content').append('<p>这是新添加的内容</p>'); }); }); </script> </body> </html>
layui The .use
method loads jQuery, and then adds a <p>
tag to the #content
element through jQuery’s append
method. When the button is clicked, content is dynamically added to the page. <p>Example 2: Using jQuery events<p>In addition to adding elements, we can also use jQuery events in Layui to implement interactive functions. Here is an example: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>使用jQuery事件</title> <link rel="stylesheet" href="layui/css/layui.css"> <script src="jquery.min.js"></script> <script src="layui/layui.js"></script> </head> <body> <div class="layui-container"> <button class="layui-btn" id="clickBtn">点击我</button> </div> <script> layui.use(['jquery', 'layer'], function () { var $ = layui.$; $('#clickBtn').on('click', function () { layer.msg('您点击了按钮'); }); }); </script> </body> </html>
layer.msg
method to pop up a prompt box, which is triggered when the button is clicked. Through this example, we can see how to use jQuery events to interact in Layui.
<p>Summary:
<p>Through the above two examples, we can see that using ordinary jQuery methods in Layui is extremely simple. You only need to introduce jQuery and Layui library files into the page, and then use jQuery's syntax in Layui's module loading to easily implement various functions. I hope this article can help you better use common jQuery methods in Layui projects. The above is the detailed content of How to utilize jQuery regular methods in Layui?. For more information, please follow other related articles on the PHP Chinese website!