如何在js或js文件中引入其他js文件(动态引入js文件)

示例如下:通过document.createElement,动态将js文件引入

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div>
        <button onclick="submit()">click</button>
    </div>
</body>
<script>
    function submit() {
        var link = document.createElement('link');
        link.rel = "stylesheet";
        link.href = "https://cdn.jsdmirror.com/gh/MCCRNFC-SNLoong/LayuiCdn/layui-v2.7.6/css/layui.css";
        document.head.appendChild(link);

        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = 'https://cdn.jsdmirror.com/gh/MCCRNFC-SNLoong/LayuiCdn/layui-v2.7.6/layui.js';

        script.onload = function () {
                console.log('脚本加载完成');
                var html = `
                <div>
                <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">充值账号</label>
                    <div class="layui-input-inline">
                        <input type="tel" name="text" required autocomplete="off" class="layui-input" value="zhangsan" disabled style="color:green" >
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">充值金额</label>
                    <div class="layui-input-inline" style="width: 100px;">
                        <input type="text" name="price_min" placeholder="¥" autocomplete="off" required class="layui-input" value ="100" disabled style="color:red">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">充值平台</label>
                <div class="layui-input-block">
                    <select name="interest" lay-filter="aihao">
                        <option value="0">微信</option>
                        <option value="1" selected="">支付宝</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit="" onclick="return  layer.load(2, {time: 3*1000});" >立即提交</button>
                </div>
            </div>
                </div>
            `;
            layer.open({
                type: 1,
                title: '充值',
                content: html
            });
        };
        document.head.appendChild(script);
    }
</script>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值