在子单击jquery上隐藏父项

时间:2012-09-02 06:20:25

标签: java html5 jquery

  

可能重复:
  jQuery onclick hide its parent element

我想在有人点击其子<li>时隐藏<a>。我使用以下jQuery代码来执行操作,但它无法正常工作。因为如果有人点击<a>这是Twitter按钮,那么首先要打电话给“ twitter-follow-button ”。它不适用于jQuery操作。 使用的jQuery:

$(document).ready(function(e) {
     $('.twitter-follow-button').click(function() {
            $(this).parent().hide();
     });
});

使用的HTML:

 <ul>
   <li>
       <div>Something</div>
       <p>Something</p>
       <a href="https://twitter.com/'.$uname.'" class="twitter-follow-button">Follow </a>
   </li>
   <li>
       <div>Something</div>
       <p>Something</p>
       <a href="https://twitter.com/'.$uname.'" class="twitter-follow-button">Follow</a>
   </li>
</ul>

1 个答案:

答案 0 :(得分:1)

您要解决的问题并不完全清楚。如果您要执行的操作是在单击链接时阻止默认操作并仅执行隐藏操作,则可以执行以下操作:

$(document).ready(function(e) {
     $('.twitter-follow-button').click(function() {
            $(this).parent().hide();
            return false;   // prevent default action of the click
     });
});

或者,如果您想在其他操作运行的同时将隐藏操作延迟一段时间,您可以这样做:

$(document).ready(function(e) {
     $('.twitter-follow-button').click(function() {
            var self = this;
            setTimeout(function() {
                $(self).parent().hide();
            }, 1000);   // you pick the appropriate time here
     });
});
相关问题