{"id":344265,"date":"2021-02-09T00:55:10","date_gmt":"2021-02-08T16:55:10","guid":{"rendered":"http:\/\/4563.org\/?p=344265"},"modified":"2021-02-09T00:55:10","modified_gmt":"2021-02-08T16:55:10","slug":"%e5%af%b9-layui-%e7%9a%84%e4%ba%8b%e4%bb%b6%e7%9b%91%e5%90%ac%e6%9c%89%e4%ba%9b%e7%96%91%e9%97%ae%ef%bc%8c%e6%b1%82%e5%b8%ae%e5%bf%99%e5%88%86%e6%9e%90%e4%b8%80%e4%b8%8b%e8%bf%99%e5%87%a0%e8%a1%8c","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=344265","title":{"rendered":"\u5bf9 Layui \u7684\u4e8b\u4ef6\u76d1\u542c\u6709\u4e9b\u7591\u95ee\uff0c\u6c42\u5e2e\u5fd9\u5206\u6790\u4e00\u4e0b\u8fd9\u51e0\u884c\u4ee3\u7801"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5bf9 Layui \u7684\u4e8b\u4ef6\u76d1\u542c\u6709\u4e9b\u7591\u95ee\uff0c\u6c42\u5e2e\u5fd9\u5206\u6790\u4e00\u4e0b\u8fd9\u51e0\u884c\u4ee3\u7801               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : he2020 <\/span>  <span><i><\/i> 5<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p><b>\u5148\u770b\u4e0b Layui \u6587\u6863\u7684\u63cf\u8ff0\uff0c\u6bd4\u5982\u76d1\u542c\u9009\u9879\u5361\u5207\u6362\u8fd9\u4e00\u8282\uff1a<\/b><br \/> Tab \u9009\u9879\u5361\u70b9\u51fb\u5207\u6362\u65f6\u89e6\u53d1\uff0c\u56de\u8c03\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a object \u53c2\u6570\uff0c\u643a\u5e26\u4e24\u4e2a\u6210\u5458\uff1a<\/p>\n<pre><code>element.on('tab(filter)', function(data){   console.log(this);        \/\/\u5f53\u524d Tab \u6807\u9898\u6240\u5728\u7684\u539f\u59cb DOM \u5143\u7d20   console.log(data.index);  \/\/\u5f97\u5230\u5f53\u524d Tab \u7684\u6240\u5728\u4e0b\u6807   console.log(data.elem);   \/\/\u5f97\u5230\u5f53\u524d\u7684 Tab \u5927\u5bb9\u5668 }); <\/code><\/pre>\n<p><b>\u7591\u95ee\u5728\u4e8e\uff1a<\/b><br \/> \u5982\u679c\u6211\u4eec\u7528\u6bd4\u8f83\u539f\u59cb\u7684\u65b9\u6cd5\u6765\u76d1\u542c Tab \u9009\u9879\u5361\u70b9\u51fb\u5207\u6362\u65f6\u89e6\u53d1\uff0c\u53ef\u80fd\u662f\u8fd9\u6837\u5199\uff1a<\/p>\n<pre><code>eleTab.addEventListener(\"click\", function(event) {  \/\/ \u70b9\u51fb Tab \u9009\u9879\u5361\u65f6\u89e6\u53d1\u8be5\u51fd\u6570 }) <\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\u4e0e Layui \u7684\u5199\u6cd5\u4ece\u5916\u89c2\u5f62\u5f0f\u4e0a\u5c31\u6709\u5f88\u5927\u7684\u5dee\u522b\u3002 Layui \u505a\u4e86\u54ea\u4e9b\u5c01\u88c5\u5462\uff1f<b>Layui \u4e0a\u9762\u7684\u5199\u6cd5\u5e76\u6ca1\u6709\u770b\u5230\u5b83\u76d1\u542c click \u4e8b\u4ef6\u554a\uff0c\u4e3a\u5565\u5728\u70b9\u51fb Tab \u9009\u9879\u5361\u65f6\u4f1a\u89e6\u53d1\uff1f<\/b><br \/> \u6211\u89c9\u5f97\u53ef\u80fd\u662f\u4e0e\u4ee5\u4e0b\u6e90\u7801\u6709\u5173\uff0c\u4f46\u770b\u4e0d\u51fa\u91cd\u70b9\u3002<\/p>\n<p> <b>element.js<\/b> https:\/\/github.com\/sentsin\/layui\/blob\/master\/src\/lay\/modules\/element.js#L29<\/p>\n<pre><code>  Element.prototype.on = function(events, callback){     return layui.onevent.call(this, MOD_NAME, events, callback);   }; <\/code><\/pre>\n<p><b>layui.js<\/b> https:\/\/github.com\/sentsin\/layui\/blob\/743d498816651899a339d6e88210f6513f2b61d1\/src\/layui.js#L563<\/p>\n<pre><code>  \/\/\u81ea\u5b9a\u4e49\u6a21\u5757\u4e8b\u4ef6   Layui.prototype.onevent = function(modName, events, callback){     if(typeof modName !== 'string'      || typeof callback !== 'function') return this;      return Layui.event(modName, events, null, callback);   };    \/\/\u6267\u884c\u81ea\u5b9a\u4e49\u6a21\u5757\u4e8b\u4ef6   Layui.prototype.event = Layui.event = function(modName, events, params, fn){     var that = this     ,result = null     ,filter = (events || '').match(\/((.*))$\/)||[] \/\/\u63d0\u53d6\u4e8b\u4ef6\u8fc7\u6ee4\u5668\u5b57\u7b26\u7ed3\u6784\uff0c\u5982\uff1aselect(xxx)     ,eventName = (modName + '.'+ events).replace(filter[0], '') \/\/\u83b7\u53d6\u4e8b\u4ef6\u540d\u79f0\uff0c\u5982\uff1aform.select     ,filterName = filter[1] || '' \/\/\u83b7\u53d6\u8fc7\u6ee4\u5668\u540d\u79f0,\uff0c\u5982\uff1axxx     ,callback = function(_, item){       var res = item &amp;&amp; item.call(that, params);       res === false &amp;&amp; result === null &amp;&amp; (result = false);     };          \/\/\u5982\u679c\u53c2\u6570\u4f20\u5165\u7279\u5b9a\u5b57\u7b26\uff0c\u5219\u6267\u884c\u79fb\u9664\u4e8b\u4ef6     if(params === 'LAYUI-EVENT-REMOVE'){       delete (that.cache.event[eventName] || {})[filterName];       return that;     }          \/\/\u6dfb\u52a0\u4e8b\u4ef6     if(fn){       config.event[eventName] = config.event[eventName] || {};        \/\/\u8fd9\u91cc\u4e0d\u518d\u5bf9\u591a\u6b21\u4e8b\u4ef6\u76d1\u542c\u505a\u652f\u6301\uff0c\u907f\u514d\u66f4\u591a\u9ebb\u70e6       \/\/config.event[eventName][filterName] ? config.event[eventName][filterName].push(fn) :        config.event[eventName][filterName] = [fn];       return this;     }          \/\/\u6267\u884c\u4e8b\u4ef6\u56de\u8c03     layui.each(config.event[eventName], function(key, item){       \/\/\u6267\u884c\u5f53\u524d\u6a21\u5757\u7684\u5168\u90e8\u4e8b\u4ef6       if(filterName === '{*}'){         layui.each(item, callback);         return;       }              \/\/\u6267\u884c\u6307\u5b9a\u4e8b\u4ef6       key === '' &amp;&amp; layui.each(item, callback);       (filterName &amp;&amp; key === filterName) &amp;&amp; layui.each(item, callback);     });          return result;   }; <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>8<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5305448\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : he2020 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Layui \u76d1\u542c\u9009\u9879\u5361\u5207\u6362\uff0c\u6587\u6863\u4f4d\u4e8e\uff1a https:\/\/www.layui.com\/doc\/modules\/element.html#ontab                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5305449\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Rhilip <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5982\u679c\u4e0d\u662f\u539f\u751f\u7684 addEventListener\uff0c\u8003\u8651\u5230 layui \u57fa\u4e8e jquery\uff0c\u5e94\u8be5\u4ece .on(&#8216;click&#8217;, callback) \u6216\u8005 .click(callback) \u7684\u89d2\u5ea6\u5165\u624b\u641c\u7d22\uff0c\u8fd9\u6837\u4f60\u5c31\u4f1a\u53d1\u73b0<\/p>\n<p>https:\/\/github.com\/sentsin\/layui\/blob\/743d498816651899a339d6e88210f6513f2b61d1\/src\/lay\/modules\/element.js#L72-L78<\/p>\n<p>&#8220;`<br \/> \/\/\u81ea\u5b9a\u4e49 Tab \u9009\u9879\u5361<br \/> Element.prototype.tab = function(options){<br \/> options = options || {};<br \/> dom.on(&#8216;click&#8217;, options.headerElem, function(e){<br \/> var index = $(this).index();<br \/> call.tabClick.call(this, e, index, null, options);<br \/> });<br \/> };<br \/>&#8220;`<\/p>\n<p>\u6b64\u5904\u4e3a tabClick \u6dfb\u52a0\u4e86\u4e00\u4e2a click \u4e8b\u4ef6\uff0c\u4e8b\u4ef6\u6700\u7ec8\u89e6\u53d1\u4e86 call.tabClick \u65b9\u6cd5\uff0c\u5e76\u5728\u6b64\u65b9\u6cd5\u4e2d\u5b9e\u73b0\u4e86\u9009\u9879\u5361\u5207\u6362                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5305450\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : azcvcza <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u770b<br \/>&#8220;`<br \/>\/\/\u6267\u884c\u81ea\u5b9a\u4e49\u6a21\u5757\u4e8b\u4ef6<br \/>filter = (events || &#8221;).match(\/..\/) \/\/ \u63d0\u53d6\u4e8b\u4ef6\u8fc7\u6ee4\u5668\u5b57\u7b26\u7ed3\u6784 \u5982 select(xxx)<br \/>filterName = filter[1] || &#8221;;<br \/>&#8230;<\/p>\n<p>\/\/ \u6267\u884c\u6307\u5b9a\u4e8b\u4ef6<br \/>filterName &amp;&amp; key === filterName &amp;&amp; layui.each(item,callback)<br \/>&#8220;`<br \/>on &#8220;tab(filter)&#8221; \u548c &#8220;select(xxx&#8217;)&#8221; \u6211\u770b\u6ca1\u5565\u533a\u522b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5305451\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : he2020 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Rhilip \u611f\u8c22\uff0c\u4f46 layui \u7684\u6587\u6863\u4e2d\u5199\u9053\uff1a\u5143\u7d20\u7684\u4e00\u4e9b\u4e8b\u4ef6\u76d1\u542c\uff0c\u4f9d\u9760\u7684\u662f element.on(filter, callback); \u554a \uff0celement.on(&#8230;)\u4e3a\u5565\u80fd\u7528\u4e8e\u4e8b\u4ef6\u76d1\u542c\u5462\uff1f\u5e95\u5c42\u505a\u4e86\u4ec0\u4e48\u5462\uff1f <br \/>https:\/\/www.layui.com\/doc\/modules\/element.html#base                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5305452\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : loading <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5efa\u8bae\u65e9\u70b9\u5f03\u5751\uff0clayui \u5c31\u662f\u521a\u5165\u95e8\u7528\u7684\uff0c\u5982\u679c\u4f60\u9700\u8981\u6298\u817e\u8fd9\u4e9b\u7684\u65f6\u5019\uff0c\u4f60\u5e94\u8be5\u6362\u4e00\u4e2a\u5e93\u4e86\u3002<br \/>\u6211\u7528\u8fc7\u4e00\u6b21\u5c31\u6015\u4e86\uff0c\u8fd8\u4e0d\u5982\u81ea\u5df1\u7528 jquery \u4ece\u5934\u5f00\u59cb\u62fc\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5305453\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : he2020 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @loading \u7528\u6765\u5b66\u4e60                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5305454\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : loading <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u53ef\u80fd\u662f\u6211\u592a\u83dc\u4e86\u5427\uff0c\u6211\u89c9\u5f97\u5bf9\u7740 layui \u6765\u5b66\u4e1c\u897f\uff0c\u5c31\u662f\u8d70\u4e86\u6b6a\u8def\u3002\u6211\u7528 jquery \u8dd1 bootstrap \u6839\u672c\u6ca1\u6709\u90a3\u79cd\u96be\u53d7\u7684\u611f\u89c9\uff0c\u6211\u89c9\u5f97\u6211\u73b0\u5728\u7528 vue \u548c react \u90fd\u6bd4\u7528 layui \u7b80\u5355\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5305455\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Rhilip <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @he2020 <\/p>\n<p>1. Layui \u4e0a\u9762\u7684\u5199\u6cd5\u5e76\u6ca1\u6709\u770b\u5230\u5b83\u76d1\u542c click \u4e8b\u4ef6\u554a\uff0c\u4e3a\u5565\u5728\u70b9\u51fb Tab \u9009\u9879\u5361\u65f6\u4f1a\u89e6\u53d1\uff1f<br \/>2. element.on(&#8230;)\u4e3a\u5565\u80fd\u7528\u4e8e\u4e8b\u4ef6\u76d1\u542c\u5462\uff1f\u5e95\u5c42\u505a\u4e86\u4ec0\u4e48\u5462\uff1f<\/p>\n<p>\u6211\u89c9\u5f97\u4e0a\u9762\u4e24\u4e2a\u95ee\u9898\u53ef\u4ee5\u4e00\u8d77\u56de\u7b54\u3002\u9996\u5148 https:\/\/github.com\/sentsin\/layui\/blob\/743d498816651899a339d6e88210f6513f2b61d1\/src\/lay\/modules\/element.js#L466 <br \/>\u5728\u52a0\u8f7d elements.js \u65f6\u5c31\u5df2\u7ecf\u4f7f\u7528 jquery \u65b9\u6cd5\u7ed9 Tab \u6dfb\u52a0\u4e86 click \u4e8b\u4ef6\uff0c\u4e8b\u4ef6\u7684\u56de\u8c03\u662f call.tabClick<br \/>\u5728 call.tabClick \u4e2d <br \/>&#8220;`<br \/> layui.event.call(this, MOD_NAME, &#8216;tab(&#8216;+ filter +&#8217;)&#8217;, {<br \/> elem: parents<br \/> ,index: index<br \/> });<br \/>&#8220;`<br \/>\u53c8\u8c03\u7528\u4e86 layui.event.call \u51fd\u6570\u6765\u8c03\u7528\u4f60\u4e4b\u524d\u4f7f\u7528 element.on(&#8230;) \u65b9\u6cd5\u5b9a\u4e49\u7684\u4e8b\u4ef6\uff08\u5b58\u5728\u5e95\u5c42 config.event \u5217\u8868\uff09\u3002<br \/>\u6240\u4ee5\uff0c\u4ed6\u53ea\u662f\u5bf9 addEventListener \u8fdb\u884c\u4e86\u4e00\u5c42\u4ee3\u7406\uff0c\u6765\u4fdd\u8bc1\u4e8b\u4ef6\u987a\u5e8f\uff0c\u5b9e\u8d28\u662f\u843d\u5b9e\u5230 jquery.on() \u4e0a\uff0c\u518d\u7531 jquery \u518d\u56de\u5230\u6d4f\u89c8\u5668\u539f\u751f\u7684 addEventListener                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5bf9 Layui \u7684\u4e8b\u4ef6\u76d1\u542c\u6709\u4e9b\u7591\u95ee&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/344265"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=344265"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/344265\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=344265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=344265"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=344265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}