javascript - 我无法获得定位父 div 和父 sibling 的按钮

标签 javascript jquery html css

我的 jquery 技能很弱,这让我很沮丧。最后期限迫在眉睫。所以,我有这个 html:

<div class="productbox"><button>1</button></div>
<div class="productbox"><button>2</button></div>
<div class="productbox"><button>3</button></div>
<div class="productbox"><button>4</button></div>
<div class="productbox"><button>5</button></div>
<div class="productbox"><button>6</button></div>
<div class="productbox"><button>7</button></div>

使用这个函数

$(function() {
$(".productbox").click(function() {
    $(this).toggleClass( "expandthebox", 1500 ),   
   $(".productbox").not(this).removeClass("expandthebox", 1500); 
    return false;
  });
});

效果很好,但点击是在 div 上,而不是在 div 内的按钮上。我希望 div 像单击 div 内的按钮(或图片)一样展开。我已经接近于通过单击“按钮”和 $(this).parent.toggleclass 使其工作,但我似乎无法让“removeClass”在其他 div 上工作。 现在又累又暴躁,真的希望有人能帮助我。提前致谢。

最佳答案

这个怎么样

$(function() {
    $(".productbox button").click(function() {
        var $parentDiv = $(this).closest('.productbox');
        $parentDiv.toggleClass( "expandthebox", 1500 ),   
        $(".productbox").not($parentDiv).removeClass("expandthebox", 1500); 
        return false;
    });
});

工作 fiddle

关于javascript - 我无法获得定位父 div 和父 sibling 的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27223965/

相关文章:

javascript - 如何重构javascript中的条件逻辑?

javascript - Angular JS 在可编辑模式下添加新行

javascript - 在最后一张幻灯片处停止幻灯片

javascript - 文档高度未达到全高

javascript - 如何使用 jquery 获取数据以 Bootstrap 3 模态形式

javascript - onclick 函数有未定义的调用者对象

javascript - Jquery .animate 无意行为

javascript - Bootstrap Nav 折叠禁用

css - 设计一个只允许内容滚动的网页 - Dreamweaver

html - 悬停字段的尺寸很小