jQuery onclick添加左边距 [英] jquery onclick add margin-left

查看:190
本文介绍了jQuery onclick添加左边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在JS中尝试一些非常简单的方法,但是我无法使其工作...

I'm trying something pretty simple in JS but I can't make it work...

我想在一个div上单击以向另一个div添加负的左边距,但是我希望每次单击div时都会发生这种情况,而不仅仅是现在这样. Eveytime我单击我的#next_nav,我希望#nav从10px负值移动. 在这里它只能工作一次. 这是我的js:

I would like when clicking on a div to add a negative margin-left to another div, but I want it to happened everytime I click on the div, not only one time as it does now. Eveytime I click on my #next_nav, I would like the #nav to move from 10px negative. here it only works one time. here is my js :

$(function() {
  $('#next_nav').click(function () {
     $( "#nav" ).css('margin-left','-10px');
  });
});

和我的HTML:

<div id="next_nav"></div>
<div id="nav"></div>

这是我的JSfiddle: http://jsfiddle.net/Beyzd/

here is my JSfiddle : http://jsfiddle.net/Beyzd/

有人可以帮助我吗? 非常感谢,

can anybody helps me with this ? thanks a lot,

推荐答案

在您的值前面添加=:

$(function() {
    $('#next_nav').click(function() {
       $('#nav').css('margin-left', '-=10px');
    });
});

工作小提琴

Working Fiddle

编辑

如果要为其设置动画,请使用animate()方法.这是给您的小提琴.

If you want to animate it, use animate() method. Here is a fiddle for you.

这篇关于jQuery onclick添加左边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆