生活随笔

vuePress-theme-reco Warn    2017 - 2023
生活随笔

Choose mode

  • dark
  • auto
  • light
首页
分类
  • book
  • DedeCMS
  • form
  • HTML5
  • layui
  • jQuery
  • study
  • Mac
  • Node
  • Office
  • Browser
  • Images
标签
时间轴
联系
  • GitHub
  • WeChat

Warn

26

文章

38

标签

首页
分类
  • book
  • DedeCMS
  • form
  • HTML5
  • layui
  • jQuery
  • study
  • Mac
  • Node
  • Office
  • Browser
  • Images
标签
时间轴
联系
  • GitHub
  • WeChat
  • layui use 定义js外部引用函数

    • 写在最后

    layui use 定义js外部引用函数

    vuePress-theme-reco Warn    2017 - 2023

    layui use 定义js外部引用函数


    Warn 2018-02-26 21:39:55 layui

    layui.use 加载layui.define 定义的模块,当外部 js 或 onclick调用 use 内部函数时,需要在 use 中定义 window 函数供外部引用 ,如下:

    layui.use(['layer','form'],function(){
       var layer = layui.layer,
           form = layer.form();
       
       var Test = function(){
          //不能被外部引用
          console.log("call Test");
       }
    
    
       window.Hello = function(){
           //可以被外部引用
           console.log("call hello");
       }
    
       Test();  //执行成功
       Hello();  //执行成功
    });
    
    
    
    $(function(){
        Hello();  //可以调用
        Test();   //提供未找到 Test
    })
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25

    注:需要引用 layui.all.js或者layui.js

    # 写在最后

    转载来源:layui use 定义js外部引用函数