如何使下面的javascript函数成为全局函数?
我已经为HTML和JS代码分离了文件。。我想做一个函数,当我用另一个(像一个图像数组(?)点击图像时,它允许我改变图像 html js链接如下所示:如何使下面的javascript函数成为全局函数?,javascript,html,Javascript,Html,我已经为HTML和JS代码分离了文件。。我想做一个函数,当我用另一个(像一个图像数组(?)点击图像时,它允许我改变图像 html js链接如下所示: <script src="file.js"></script> <article> <h3>...</h3> <figure> <img src="images/image7.jpg" alt="path not found" class="dynami
<script src="file.js"></script>
<article>
<h3>...</h3>
<figure>
<img src="images/image7.jpg" alt="path not found" class="dynamic-image" onclick="changeImg(1)">
<figcaption></figcaption>
</figure>
<figure>
<img src="images/image10.jpg" alt="path not found" class="dynamic-image" onclick="changeImg(0)">
<figcaption></figcaption>
</figure>
</article>
我希望能够使用如下功能:
<script src="file.js"></script>
<article>
<h3>...</h3>
<figure>
<img src="images/image7.jpg" alt="path not found" class="dynamic-image" onclick="changeImg(1)">
<figcaption></figcaption>
</figure>
<figure>
<img src="images/image10.jpg" alt="path not found" class="dynamic-image" onclick="changeImg(0)">
<figcaption></figcaption>
</figure>
</article>
...
因此,为了能够使用onclick=“changeImg(param)”。。。
JS代码如下所示:
window.onload = function() {function changeImg(param) {
//function changeImg(param) {
// window.changeImg = function(param){
//The purpose of "use strict" is to indicate that the code should be executed in "strict mode".
// With strict mode, you can not, for example, use undeclared variables.
'use strict';
var preloads=[],c;
//The preload function ensures that the images have all been loaded,
// so that there’s no delay while the image is being loaded (as it’s already been preloaded) when the next image is used.
function preload(){
//arguments is an Array-like object accessible inside functions that contains the values of the arguments passed to that function.
for(c=0;c<arguments.length;c++) {
preloads[preloads.length] = new Image();
preloads[preloads.length-1].src = arguments[c];
}
c=0;
}
//cha hakyeon
if (param == 0){
preload('----src1---');
}
else if(param == 1){
preload('----src2---');
}
//The last function occurs on the click event, which increases a counter and changes the image to the next image.
document.getElementsByClassName('dynamic-image').addEventListener('click',
function() {
c++;
if(c == preloads.length) {
c = 0;
}
this.src = preloads[c].src;
});
}
}(param);
window.onload=function(){function changeImg(param){
//函数更改img(参数){
//window.changeImg=函数(参数){
//“使用严格”的目的是指示代码应在“严格模式”下执行。
//例如,在严格模式下,不能使用未声明的变量。
"严格使用",;
var预载=[],c;
//预加载功能确保图像已全部加载,
//因此,在使用下一个图像时,在加载图像时(因为它已经预加载)没有延迟。
函数预加载(){
//arguments是一个类似数组的对象,可在函数内部访问,它包含传递给该函数的参数值。
对于(c=0;c您需要在全局范围内声明您的changeImg
函数
试试这个:
功能更改img(参数){
//函数更改img(参数){
//window.changeImg=函数(参数){
//“使用严格”的目的是指示代码应在“严格模式”下执行。
//例如,在严格模式下,不能使用未声明的变量。
"严格使用",;
var预载=[],c;
//预加载功能确保图像已全部加载,
//因此,在使用下一个图像时,在加载图像时(因为它已经预加载)没有延迟。
函数预加载(){
//arguments是一个类似数组的对象,可在函数内部访问,它包含传递给该函数的参数值。
对于(c=0;c
ETA:这是整个脚本,您不需要窗口。onload
赋值
此外,您还遇到了一个语法错误,导致脚本无法完全加载(在我的例子中,这就是导致脚本无法全局访问的原因)
它位于这一行preload('----src2---');
(缺少一个引号)您的函数changeImg是在函数窗口中定义的。onload,创建一个闭包。这意味着函数changeImg只在该函数作用域内可用,我们不知道该函数外有什么changeImg
解决这个问题的一个快速方法是在window.onload函数之外定义changeImg函数。检查控制台是否有错误单引号错误!复制错误,将对其进行编辑。嗯,因此现在在file.js中我有以下内容:函数changeImg(param){…}
调用它我认为这样做正确吗?
现在我的js文件是这样的:函数changeImg(param){'use strict';var preload=[],c;[…]}
现在我应该在html标记中这样调用它吗?