导入和导出jquery函数 [英] import and export a jquery function

查看:144
本文介绍了导入和导出jquery函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带jquery代码的custom.js文件:

I have a custom.js file with jquery code :

$('.nav.navbar-nav > li').on('click', function(e) {
$('.nav.navbar-nav > li').removeClass('active');
$(this).addClass('active');
});

我想将此代码导出到main.js文件。这两个文件位于同一个文件夹中。

I want to export this code to the main.js file.Both files are in the same folder.

通过文档但无法理解如何使用jquery。

Went through the documentation but couldn't understand how to do it with jquery.

推荐答案

首先你需要将此代码包装在一个函数中。然后导出该功能。最后你在main.js中导入它:

First you need to wrap this code in a function. Then you export the function. Finally you import it in main.js:

custom.js

custom.js

export function foo () {
    $('.nav.navbar-nav > li').on('click', function(e) {
        $('.nav.navbar-nav > li').removeClass('active');
        $(this).addClass('active');
    });
}

main.js

import {foo} from 'custom'

foo();

ECMA SCRIPT 6模块

另请注意,您需要一个模块加载器(如webpack / systemjs / requirejs等),以及将您的ES6代码转换为ES5的Babel。

Also notice that you need a module loader (like webpack/systemjs/requirejs etc.), as well as Babel that transpiles your ES6 code to ES5.

这篇关于导入和导出jquery函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆