按钮没有响应

我是注册用户

我有一个按钮,显示“顶部添加”,当我单击该按钮时没有响应。假定要做的是警告一个刚添加到的变量,但是它什么也没做。这是我的代码:

$("button#left").click(function(){
    moveLeftId[moveLeftId.length]="-=50px";
    alert(moveLeftId);
});


$("button#start").click(function(){
    alert("You clicked action!");
    var i;
    for (i = 0; i < moveLeftId.length; i++) {
        $("div#test").animate({
           marginLeft: moveLeftId[i]
        }, 500);
    }
    for (i = 0; i < moveTopId.length; i++) {
        $("div#test").animate({
            marginTop: moveTopId[i]
        }, 500);
    }
});
/*
$("button#top").click(function(){
    moveTopId[moveTopId.length]="+=50px";
});marginTop: moveTopId[i]*/
#test {
    width: 100px;
    height: 100px;
    background-color: #C00;
    margin-left: 500px; 
    margin-top:300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="test" color="white">Hi</div>
<button id="left">Left add</button>
<button id="start">Action!</button>
<button id="top" onclick="moveTopId[moveTopId.length]='-=50px';
    alert(moveTopId);">Top add</button>
<script>var moveLeftId=new Array(["-=50px"]);
    var moveBottomId=new Array(["-=50px"]);</script>

如您所见,我尝试将一些代码分成其他脚本标签,但这没有什么区别。我不知道该怎么办!



提前致谢!

Shibi

您没有定义此数组,因此如果未定义Array.length,则无法获取。

您的脚本正在运行,只需要在开始时定义此变量。

http://jsfiddle.net/2xybrnrz/

var moveLeftId = [];
var moveTopId = [];

$("button#left").click(function(){
    moveLeftId[moveLeftId.length]="-=50px";
    alert(moveLeftId);
});


$("button#start").click(function(){
    alert("You clicked action!");
    var i;
    for (i = 0; i < moveLeftId.length; i++) {
        $("div#test").animate({
           marginLeft: moveLeftId[i]
        }, 500);
    }
    for (i = 0; i < moveTopId.length; i++) {
        $("div#test").animate({
            marginTop: moveTopId[i]
        }, 500);
    }
});

$("button#top").click(function(){
    moveTopId[moveTopId.length]="+=50px";
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章