无法将 JavaScript 从 HTML 文件移动到 JS 文件并将其导入应用程序.js - Rails



我有一些javascript(进度条(,在html中的脚本标签中工作正常。 但是我试图将其移动到.js文件中并导入到应用程序中.js但我不确定如何。 我之前使用另一个 JavaScript 功能完成了这一点,我将整个事情放在一个函数中,(我在这里添加了函数 bar(((然后导出它, 然后在应用程序中导入.js,但是这次它不起作用。我对JavaScript很陌生,谢谢。

function bar(){
$('.progress-wrap').each(function(){
percent = $(this);
bar = $(this).children('.progress-bar');
moveProgressBar(percent, bar);
});
// function bar(){
// on browser resize...
$(window).resize(function() {
$('.progress-wrap').each(function(){
percent = $(this);
bar = $(this).children('.progress-bar');
moveProgressBar(percent, bar);
});
});
// SIGNATURE PROGRESS
function moveProgressBar(percent, bar) {
var getPercent = (percent.data('progress-percent') / 100);
var getProgressWrapWidth = percent.width();
var progressTotal = getPercent * getProgressWrapWidth;
var animationLength = 1000;
// on page load, animate percentage bar to data percentage length
// .stop() used to prevent animation queueing
bar.stop().animate({
left: progressTotal
}, animationLength);
}
}
export {bar};

如果没有完整的 html 文件和代码,给出一个准确的答案有点困难。但在某些情况下,问题在于顺序。例如,您使用jQuery或其他目前不可用的库。

通常,您应该以这种方式更改脚本,如果其他所有内容都准备就绪,则首先运行脚本。在你的脚本中,你使用jQuery,所以你可以使用

$(document).ready(function());

然后,如果文档准备就绪,则首先运行脚本。

http://learn.jquery.com/using-jquery-core/document-ready/

您可以使用浏览器中的开发人员工具检查所有这些内容,并检查是否可以看到一些缺少某些内容的错误。

最新更新