代码之家  ›  专栏  ›  技术社区  ›  superUntitled

在javascript(jquery)中重置变量

  •  0
  • superUntitled  · 技术社区  · 14 年前

    我尝试在单击图像映射的某个区域时重置变量(更新图像的SRC值),以便当用户将鼠标悬停在图像映射的另一个区域上时,悬停函数调用不同的图像。

    我有一个名为“状态”的变量,可以直接设置。这是单击区域时要重置的变量。

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

    我有几个函数涉及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();
        });
    

    以及这些函数调用的一些函数:

    function over(image) {
        $("#feednavImg").attr("src", image);
    }
    
    function off() {
        $("#feednavImg").attr("src", state);
    }
    
    2 回复  |  直到 14 年前
        1
  •  6
  •   PetersenDidIt    14 年前

    移除 var 从前方 state 你是想“重置”它吗?放 var 变量的前面在 #Map area 单击处理程序。

        2
  •  0
  •   Diodeus - James MacFarlane    14 年前
    var state = "path/to/images/feed1.png";
    var stateReset = state
    

    在Click事件中: state = stateReset