热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

函数和.hover中的JavaScript全局变量-JavaScriptglobalvariableinfunctionand.hover

IhavethiscodeinJavaScript:我在JavaScript中有这个代码:statusdocument.getElementById(status2);

I have this code in Javascript:

我在Javascript中有这个代码:

status = document.getElementById('status2');

$('#slider > img').hover(
    function() {
        stopLoop();
        status.innerHTML = "paused";
    }, 
    function() {
        startSlider();
        status.innerHTML = "playing";
    }
);

where I look for all the images in my html that have the id slider and when I hover on then I want to add a word (paused or playing) to a span tag that has the id status2. But I don't know why the global variable is not working, the only way that I make it work is putting a local variable inside each funcion like this:

在那里我查找我的html中具有id滑块的所有图像,当我悬停在那时我想要添加一个单词(暂停或播放)到具有id status2的span标记。但我不知道为什么全局变量不起作用,我使其工作的唯一方法是在每个函数中放置一个局部变量,如下所示:

function() {
    stopLoop();
    var status = document.getElementById('status2');
    status.innerHTML = "paused";
},
function() {
    startSlider();
    var status = document.getElementById('status2');
    status.innerHTML = "playing";
}

Can anyone me why?

任何人都可以为什么?

NOTE: as I said before all works with the local variables but not setting it as a global variable.

注意:正如我之前所说,所有使用局部变量但不将其设置为全局变量。

2 个解决方案

#1


1  

Because by the time you run

因为到你跑的时候

status = document.getElementById('status2');

DOM was not ready so you get status as undefined and so it wont work further.

DOM尚未就绪,因此您将获得未定义状态,因此无法继续工作。

So either put the code in ready

所以要么准备好代码

$(document).ready(function(){
   //code goes here
})

or

Put the script at the end of file.

将脚本放在文件末尾。

#2


1  

Do add in a

添加一个

$(document).ready(function(){

});

This waits to execute the code inside until everything has finished loading. That way it shouldn't return undefined.

这等待执行内部代码,直到所有内容都加载完毕。这样它就不应该返回undefined。

ALSO

I couldn't help but noticing that you seem to be trying to give multiple items the same ID.

我忍不住注意到你似乎试图给多个项目提供相同的ID。

Don't use IDs for multiple elements. That's not how they are designed to be used, nor do they work that way.If you give multiple elements the same ID and then try and style them with CSS, it'll only style the first one. Use a class. If you use

不要将ID用于多个元素。这不是它们的设计使用方式,它们也不是那样工作的。如果你给多个元素提供相同的ID,然后尝试用CSS设置它们,那么它只会设计第一个。使用课程。如果你使用

document.getElementById();

to try and grab multiple elements with the same ID, then the script will ONLY grab the FIRST element with that ID, because, given that it is an ID, it expects only one element. If you want to work with multiple elements, use a class, and then use

尝试使用相同的ID来获取多个元素,然后脚本将仅使用该ID获取FIRST元素,因为,鉴于它是一个ID,它只需要一个元素。如果要使用多个元素,请使用类,然后使用

document.getElementsByClassName();

this will grab ALL elements with that class. So for example, say you have four span elements with the class "foo". To grab all these and change the text, do this:

这将获取该类的所有元素。例如,假设您有四个带有“foo”类的span元素。要获取所有这些并更改文本,请执行以下操作:

 elements=document.getElementsByClassName("foo");
for (i=0; i

About global and local variables, a GLOBAL variable is declared this way:

关于全局变量和局部变量,GLOBAL变量以这种方式声明:

global_variable='foo'

and a local variable is declared this way:

并以这种方式声明局部变量:

var local_variable='foo'

a Global variable can be declared anywhere in the script and be used anywhere inside the script(and even in other scripts that are attached to the same HTML file ), whereas a Local variable, if declared inside the function, can only be used inside the function, or if you declare it outside the function, it can't be accessed within the function unless you pass the variable to it.

全局变量可以在脚本中的任何位置声明,并可以在脚本内的任何位置使用(甚至可以在附加到同一HTML文件的其他脚本中使用),而Local变量,如果在函数内部声明,则只能在函数,或者如果在函数外声明它,除非将变量传递给函数,否则无法在函数内访问它。

Hope that helps!

希望有所帮助!


推荐阅读
author-avatar
寒江大哥_599
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有