如何在开源 JavaScript 文件中使用函数



我正在使用一个开源的javascript文件。 代码中有一个函数,我想在我自己的代码中使用它。我的文件结构是这样的:

function ($) {
// there is codes here
function _disable(input, v) { // I need this function 
_o(input).prop('disabled', v);
input.prop('disabled', v);
var input_div = _getInputpickerDiv(input);
if (v) {
input_div.find('.inputpicker-arrow').hide();
}
else {
input_div.find('.inputpicker-arrow').show();
}
}
// there is codes here
});

我引用了开源文件并尝试了以下代码:

<script src="assets/jquery.inputpicker.js"></script>
$("input").each(function () {
_disable($(this), false);
});

但是我收到此错误:

未捕获的引用错误: 未定义_disable

谁能帮我解决这个问题?

您可以像这样设置全局变量:

function ($) {
// there is codes here
function _disable(input, v) { // I need this function 
_o(input).prop('disabled', v);
input.prop('disabled', v);
var input_div = _getInputpickerDiv(input);
if (v) {
input_div.find('.inputpicker-arrow').hide();
}
else {
input_div.find('.inputpicker-arrow').show();
}
}
// there is codes here
// make it available outside the scope
window._disable = _disable
});

稍后像这样访问它:

_disable(...)

最新更新