简体   繁体   English

重置JavaScript中的变量(jQuery)

[英]Reset variable in javascript (jQuery)

I am trying to reset a variable (that updates the src value for an image) when an area of an image map is clicked, so that the hover function calls a different image when the user hovers over another area in the image map. 我试图在单击图像映射的某个区域时重置一个变量(更新图像的src值),以便当用户将鼠标悬停在图像映射中的另一个区域时,悬停功能会调用其他图像。

I have a variable called "state" that I set right off the bat. 我有一个名为“ state”的变量,可以立即设置。 This is the variable that I want to reset when an area is clicked. 这是单击区域时要重置的变量。

var state = "path/to/images/feed1.png";

I have a couple of functions involving hover() and click(). 我有几个涉及hover()和click()的函数。

$("#Map area").click(
    function () {
        var button = $(this).attr("id");                
        // here is where i want to reset the 'state' variable
        var state = "path/to/images/" + button + ".png";
        alert("you clicked "+button+" hello");
        return false;
    }
); 


$("#Map area").hover(
    function () {
        var button = $(this).attr("id");
        over("path/to/images/" + button + ".png");
    },
    function () {
        off();
    });

And some functions that these functions call: 以及这些函数调用的一些函数:

function over(image) {
    $("#feednavImg").attr("src", image);
}

function off() {
    $("#feednavImg").attr("src", state);
}

Remove the var from infront of state were you are trying to 'reset' it. 如果要“重置”它,请从state最前面删除该var Putting var infront of the variable defines it in the scope of the #Map area click handler. var放在变量的#Map area可将其定义在#Map area单击处理程序的范围内。

var state = "path/to/images/feed1.png";
var stateReset = state

in the click event: state = stateReset 在点击事件中: state = stateReset

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM