get attribute value of a newly prepended element
I have the following html:
<ul><li rel="3">text<li><li rel="2">开发者_运维知识库text<li><li rel="1">text<li></ul>
then I prepend a new li element:
$("ul li:first").prepend('<li rel="4">text</li>');
Problem:
when I get the value of the attribute(rel) of the newly added element like this:
alert( $('ul li:first').attr('rel') );
..I keep getting "3" instead of 4...but I need the new one!
Hope somebody can help me!
TIA
Use this to prepend the new li element, it will work fine.
$("ul").prepend('<li rel="4">text</li>');
you are prepending to the child element (the li) .
you'd need to do a
$("ul").prepend('<li rel="4">text</li>');
Also, check those closing <li>
, should be </li>
prepend inserts element as first child in your object, so when you use prepend use it
$('ul').prepend('<li rel="4">text</li>');
Your selector is wrong in the call to prepend
. Do this instead:
$('ul').prepend('<li rel="4">text</li>');
Beforehand, you were prepending an <li>
to the first <li>
, not to the <ul>
.
精彩评论