cross-browser solution for changing content of last li element
I have following un-ordered list structure
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
Now, I need to replace Milk with Buttermilk, How can I do it, using jQuery ofcourse? I need solution, which works on every browser, sta开发者_如何学编程rting with IE7+
use the :last
selector
$('li').filter(':last').text('Buttermilk')
$("ul li:last").html("Buttermilk");
$('li:last').html('Buttermilk');
http://jsfiddle.net/jasongennaro/3duZK/
From the jQuery api documentation:
:last
selects a single element by filtering the current jQuery collection and matching the last element within it.
When .html()
is used to set an element's content, any content that was in that element is completely replaced by the new content.
$('ul').find('li:last').html('Buttermilk');
精彩评论