Javascript 如何手动调用某些库附带的函数
我已经安装了一个lightbox插件,我想在我的应用程序的其他部分重新使用它的一些功能,而不必重新编码一个单独的模块,因为它有点冗余,所以是否可以重用/共享库中的一些功能,比如说从这个库中 我的意图是这个灯箱插件已经支持热键的下一个,上一个,关闭等 情况是这样的。。。形成lightbox.js。。。 我想在我的应用程序的另一部分重用这个特定的函数Javascript 如何手动调用某些库附带的函数,javascript,Javascript,我已经安装了一个lightbox插件,我想在我的应用程序的其他部分重新使用它的一些功能,而不必重新编码一个单独的模块,因为它有点冗余,所以是否可以重用/共享库中的一些功能,比如说从这个库中 我的意图是这个灯箱插件已经支持热键的下一个,上一个,关闭等 情况是这样的。。。形成lightbox.js。。。 我想在我的应用程序的另一部分重用这个特定的函数 (function($) { $.fn.lightBox = function(settings) { 。。。。 ...... ...... ..
(function($) {
$.fn.lightBox = function(settings) {
。。。。
......
......
..... 等等
function _keyboard_action(objEvent) {
// To ie
if (objEvent == null) {
keycode = event.keyCode;
escapeKey = 27;
// To Mozilla
} else {
keycode = objEvent.keyCode;
escapeKey = objEvent.DOM_VK_ESCAPE;
}
// Get the key in lower case form
key = String.fromCharCode(keycode).toLowerCase();
// Verify the keys to close the ligthBox
if ((key == settings.keyToClose) || (key == 'x') || (keycode == escapeKey)) {
_finish();
}
// Verify the key to show the previous image
if ((key == settings.keyToPrev) || (keycode == 37)) {
// If we´re not showing the first image, call the previous
if (settings.activeImage != 0) {
settings.activeImage = settings.activeImage - 1;
_set_image_to_view();
_disable_keyboard_navigation();
}
}
// Verify the key to show the next image
if ((key == settings.keyToNext) || (keycode == 39)) {
// If we´re not showing the last image, call the next
if (settings.activeImage != (settings.imageArray.length - 1)) {
settings.activeImage = settings.activeImage + 1;
_set_image_to_view();
_disable_keyboard_navigation();
}
}
}
};
。。。。
......
......
..... 等等
function _keyboard_action(objEvent) {
// To ie
if (objEvent == null) {
keycode = event.keyCode;
escapeKey = 27;
// To Mozilla
} else {
keycode = objEvent.keyCode;
escapeKey = objEvent.DOM_VK_ESCAPE;
}
// Get the key in lower case form
key = String.fromCharCode(keycode).toLowerCase();
// Verify the keys to close the ligthBox
if ((key == settings.keyToClose) || (key == 'x') || (keycode == escapeKey)) {
_finish();
}
// Verify the key to show the previous image
if ((key == settings.keyToPrev) || (keycode == 37)) {
// If we´re not showing the first image, call the previous
if (settings.activeImage != 0) {
settings.activeImage = settings.activeImage - 1;
_set_image_to_view();
_disable_keyboard_navigation();
}
}
// Verify the key to show the next image
if ((key == settings.keyToNext) || (keycode == 39)) {
// If we´re not showing the last image, call the next
if (settings.activeImage != (settings.imageArray.length - 1)) {
settings.activeImage = settings.activeImage + 1;
_set_image_to_view();
_disable_keyboard_navigation();
}
}
}
};
})(jQuery)
但是如果我从外部调用:_keyboard_action(objEvent),我会得到一个错误,那么正确的语法是什么呢?根据我从这篇文章中理解的(如果我没有被误解),您可以简单地使用
<script src="NameOfJavascriptFile.js" type="text/javascript">
在html
部分中,然后像调用普通函数一样调用函数。不,这不是我的意思,这只是包括库,没有问题,问题是我如何调用methodsCall函数,就像调用脚本中的任何其他普通函数一样。您还必须放置结束标记,除非它不会出现,否则脚本将不会加载。你不能只使用
或
。答案不太正确。。。好的,让我再清楚一次。。。请参考我在上面编辑的帖子,我想,_键盘_动作是lightbox的私有功能,所以你不能访问它。要使其可重用,您需要修改此实现,或者可以复制此逻辑以生成自己的函数。