开发者

How to remove the last <li></li> using Jquery?

  <div id="q">
    <ul><li>
    <table><tr><td>Question 1</td><td><input type="text" name="question1" size="60" /><开发者_JAVA百科/td></tr>
   <tr><td>Answer:</td><td><input type="text" name="answer1" size="8" /></td></tr>
    </li></ul>
</div>

<input class="btn" id="addquestion"  type="button"  value="Add Question" />
<input class="btn" id="removequestion"  type="button"  value="Remove Question" />
<script>


 $('#addquestion').click(function() {
 var $question_number = $('#q li').size() + 1;

   $html='<li><table><tr><td>Question '+$question_number+'</td><td><input type="text" name="question'+$question_number+'" size="60" /></td></tr>\
   <tr><td>Answer:</td><td><input type="text" name="answer'+$question_number+'" size="8" /></td></tr></li>';
  $('#q ul').append($html);
   });

  $('#addquestion').click(function() {
$('#q li:last').remove();
}); 
</script>

$('#q li:last').remove(); doesn't work. What's wrong?


If you want to remove the last li element inside #d, you can simply use the :last selector:

$('#q li:last').remove();

If you want to remove every last li child of it's immediate parent (ul elements if you have more than one), you can use the :last-child selector:

$('#q li:last-child').remove();


Try:

var lis = $('#q ul').children(); // get all the children
$(lis.get(lis.length-1)).remove(); // remove the last one
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜