简体   繁体   English

扩展div双重效果JQuery中的扩展按钮

[英]Expand Button in Expand div double effect JQuery

small problem here. 这里的小问题。 I have an table and when I want to click on the run this table should expand and furthermore there is a expand button in the same row, which also should expand a div. 我有一个表格,当我要单击运行时,该表格应该展开,并且在同一行中还有一个展开按钮,该按钮也应该展开一个div。

The problem: When I click the row everything works fine, the div will slideIn. 问题:当我单击该行时,一切正常,div将滑入。 When I click on the button (which is in the table row) there is double effect. 当我单击按钮(位于表行中)时,会产生双重效果。 So the Contentdiv will slide in and afterwards slide out immediately. 因此,Contentdiv将滑入,然后立即滑出。 This is logical because with the button click I click both (table row + button) so the toggle effect happens 2 times, but I don't know how to solve this in a good way. 这是合乎逻辑的,因为单击按钮后,我同时单击了两个按钮(表格行+按钮),因此切换效果会发生2次,但是我不知道如何解决此问题。

<tr class='header mySlideToggler'>
<td class='align-center'>$username</td>
<td>0 %</td>
<td>22 Aug 2014</td>
<td>$done / $quantity</td>
<td>$running</td>
<td>$untouched</td>
<td>
<a href='#' class='table-icon edit' title='Edit'></a>
<a href='#' class='table-icon expand mySlideToggler' title='Expand'></a>
<a href='#' class='table-icon delete' title='Delete'></a>
</td>
</tr><tr style='padding: 0'><td colspan='7' style='padding: 0'>
<div class='slideMe' style='display: none'><br><br><br></div></td></tr>

JQuery: JQuery的:

$('.mySlideToggler').click(function( event ){
    event.preventDefault();
    $(this).closest('tr').next().find('.slideMe').slideToggle();
});

So how do I avoid the "double effect" on it. 因此,如何避免对其产生“双重影响”。

Use jQuery's event.stopPropagation() method to prevent the click event also firing on the tr element when clicking the a element: 使用jQuery的event.stopPropagation()方法来防止click事件在单击a元素时也触发tr元素:

[ event.stopPropagation ] prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event. [ event.stopPropagation ]防止事件使DOM树冒泡,从而防止任何父处理程序收到该事件的通知。

$('.mySlideToggler').click(function( event ) {
    event.stopPropagation();
    ...
});

James Donnelly give the solution. James Donnelly提供解决方案。 But maybe you can something like this : 但是也许您可以这样:

$('.mySlideToggler').click(function( event ){
    if($(this).hasClass('header')){
        event.preventDefault();
        $(this).closest('tr').next().find('.slideMe').slideToggle();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM