开发者

this jquery remove function?

i have a problem with this jquery, in the success function call, its mean to remove the `support' button, and fade in the msg, this is my jquery code:

$('.stats').delegate('.against', 'click', function(e) {
      //stop event
      e.preventDefault();
        // cache a reference to the previous <li> element
        //   since it is used more than once
       var $prevLi = $(this).closest('li').prev('li');
      //get the id
       var the_id = $prevLi.attr('id').split('_').pop();
        //the main ajax request
       $.ajax({
           context:this,
           type: "POST",
           data: "action=against&id=" + the_id,
           url: "ajax/sa.php",
          success: function (msg) {
               $prevLi.find("h2.score_down").html(msg).fadeIn();
               $(this).closest('li').next().next().find('button').remove(); $(this).remove();
           }
      });
 });

the html:

        <ul class="stats">
             <li id="topic_20" class="score">
                     <h2 class="score_up" style="color:green;">10</h2>
                <span style="text-align:center;">Supporters</span>
            </li>
            <li>
                <button type="submit" value="Actions" class="support" title="support">
                    <i></i>
                    <span>Support</span>
                </button>
            </li>


              <li id="down_20"class="score"><h2 class="score_down">20</h2><span style="text-align:center;">Against</span>

    </li>

              <li>
        <button type="submit" value="Actions" class="against" title="against">
          <i></i><span>Against</span></button>
              </li>
        </ul>
<h3 class="success"></h3>

this jquery is meant to remove the support b开发者_运维技巧utton, when clicked and the new score is meant to fade in! :)) thanks


The button is in the <li> 2 previous to the .against containing one, so your .next() calls should be .prev(), like this:

$(this).closest('li').prev().prev().find('button').remove();


Why not use $("button[type=submit]")? Or just $("button")? If you use the double prev() you're going to have to adjust your code when your markup changes. If you do that often enough, that makes making changes a nightmare later.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜