这个jquery删除功能?

发布于 2024-09-29 12:52:53 字数 1903 浏览 2 评论 0 原文

我对这个jquery有一个问题,在成功函数调用中,它的意思是删除“支持”按钮,并淡入消息,这是我的jquery代码:

$('.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();
           }
      });
 });

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>

这个jquery是为了删除支持按钮,当单击后,新乐谱就会淡入! :)) 谢谢

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 button, when clicked and the new score is meant to fade in! :)) thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丘比特射中我 2024-10-06 12:52:54

该按钮位于包含该按钮的

  • 2 之前.against 中,因此您的 .next() 调用应该是 .prev(),如下所示:
  • $(this).closest('li').prev().prev().find('button').remove();
    

    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();
    
    屌丝范 2024-10-06 12:52:54

    为什么不使用 $("button[type=submit]")?或者只是$(“按钮”)?如果你使用 double prev() ,当你的标记发生变化时,你将不得不调整你的代码。如果你经常这样做,那么以后的改变就会变成一场噩梦。

    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.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文