javascript - 我怎样才能在jquery中循环这个函数?

标签 javascript jquery html loops

    $( "#transbox1" ).one( "mouseenter", function() {
  $( this ).css( "width", "+=200" );
  $( this ).css( "height", "+=200" );
});
$( "#transbox1" ).one( "mouseleave", function() {
  $( this ).css( "width", "-=200" );
  $( this ).css( "height", "-=200" );
});
$( "#transbox2" ).one( "mouseenter", function() {
  $( this ).css( "width", "+=200" );
  $( this ).css( "height", "+=200" );
});
$( "#transbox2" ).one( "mouseleave", function() {
  $( this ).css( "width", "-=200" );
  $( this ).css( "height", "-=200" );
});
$( "#transbox3" ).one( "mouseenter", function() {
  $( this ).css( "width", "+=200" );
  $( this ).css( "height", "+=200" );
});
$( "#transbox3" ).one( "mouseleave", function() {
  $( this ).css( "width", "-=200" );
  $( this ).css( "height", "-=200" );
});
$( "#transbox4" ).one( "mouseenter", function() {
  $( this ).css( "width", "+=200" );
  $( this ).css( "height", "+=200" );
});
$( "#transbox4" ).one( "mouseleave", function() {
  $( this ).css( "width", "-=200" );
  $( this ).css( "height", "-=200" );
});

我有 4 个盒子,当鼠标进入时,盒子会变大 200px,当鼠标离开时,盒子会变小 200px。我该怎么办才能一直这样做?

最佳答案

只需向所有这些添加一个类

   <div class="myClass" id="transbox1"></div>

   $( ".myClass" ).on( "mouseenter", function() {
    $( this ).css( "width", "+=200" );
    $( this ).css( "height", "+=200" );
   });
  $( ".myClass" ).on( "mouseleave", function() {
    $( this ).css( "width", "-=200" );
    $( this ).css( "height", "-=200" );
  });

关于javascript - 我怎样才能在jquery中循环这个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29217770/

相关文章:

jquery - 使用 jquery 隐藏类名所在的 div

javascript - 如何在 jQuery Mobile 中并排显示两个文本输入?

javascript - 使用 JQuery 选择带有 ID 的 div 并在页面加载时自动单击它

javascript - 尝试用 JavaScript 编写计算器

javascript - 使用 JPlayer 音频播放器时出现问题

jquery - 将 3 个 div 与自动边距对齐

javascript - 使用 jQuery 选择变量中的内容而不是当前 DOM 中的内容?

javascript - 替换数组中特定位置的元素而不改变它

javascript - getElementById 给出结果加上字符串

javascript - 查找用户在 Google 表格中输入的变量并使用该变量从其他两个表格返回信息