最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - jQuery append() adding at beginning - Stack Overflow

matteradmin1PV0评论

I want to turn something like <sup>1</sup> into <sup><a href="..." class="...">1</a></sup> I figured the easist way would be to prepend <a href....> and append </a> but the append is not ing after the content (1)...

I know I could do a second .each() and store the 1 as a variable and then do .html(), but I figure that's not best practice since it involves another loop.

Thanks!!

/

I want to turn something like <sup>1</sup> into <sup><a href="..." class="...">1</a></sup> I figured the easist way would be to prepend <a href....> and append </a> but the append is not ing after the content (1)...

I know I could do a second .each() and store the 1 as a variable and then do .html(), but I figure that's not best practice since it involves another loop.

Thanks!!

http://jsfiddle/pqzL4/

Share asked Jul 13, 2011 at 20:27 mazlixmazlix 6,3136 gold badges35 silver badges46 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 7

@Daff's answer should solve your problem. The reason it doesn't work is because as soon as you do:

$(this).prev(".article").find("sup").prepend("<a href=\"#references" + (i + 1) + "\" class=\"references\">");

It gets added immediately. Since an unclosed tag is invalid, the browser will "correct" it by closing the tag for you.

Then you try to add a close tag to the end, which is invalid without an open tag so the browser ignores it.

I think you might want to look a jQuery wrapInner.

$('sup').wrapInner('<a href="#"></a>');

Which will wrap the text of any sup with a link.

You cannot append or prepend HTML fragments; you can only work with plete tags.

You're looking for wrap.

Description: Wrap an HTML structure around each element in the set of matched elements.

I think what you're looking for is "wrapInner()". It takes the contents of one element and wraps it with an element you pass in. Something along the lines of

$('sup').wrapInner('<a href...></a>') 

Try this:

$(this).prev(".article").find("sup").append('<a>' + $(this).prev(".article").find("sup").text() + '</a>');
Post a comment

comment list (0)

  1. No comments so far