简体   繁体   中英

adding content using append - Jquery

I am trying to add content (div) after an image using append(). All the list items are dynamically generated, I can only add class to the 'ul'.

Here's the fiddle: http://jsfiddle.net/krish7878/ggyDf/3/

<ul class="footer-8-flickr">
  <li> <a href="#"> <img src="#"></a></li>
  <li> <a href="#"> <img src="#"></a></li>
  <li> <a href="#"> <img src="#"></a></li>
<ul>

$("ul.footer-8-flickr li a img").append("<div class='overlay'><p>Content</p></div>");

May be I am missing something.

Use after instead of append .

Take a look at

Fiddle

<ul class="footer-8-flickr">
    <li> <a href="#"> <img src="#"/></a>
    </li>
    <li> <a href="#"> <img src="#"/></a>
    </li>
    <li> <a href="#"> <img src="#"/></a>
    </li>
    <li> <a href="#"> <img src="#"/></a>
    </li>
    <li> <a href="#"> <img src="#"/></a>
    </li>
    <li> <a href="#"> <img src="#"/></a>
    </li>
    <li> <a href="#"> <img src="#"/></a>
    </li>
    <li> <a href="#"> <img src="#"/></a>
    </li>
</ul>

You can't insert an element inside an img element, instead you can insert it after the img element as the next sibling

$("ul.footer-8-flickr li a img").after("<div class='overlay'><p>Content</p></div>");

Demo: Fiddle

You can use .after() to do this.

You cannot Insert content in an img element.

Try this

$("ul.footer-8-flickr li a").append("<div class='overlay'><p>Content</p></div>");

It'll add div after img tag as a sibling.

你不能追加img元素,因为它是自关闭标签,而不是将其附加到a元素。

$("ul.footer-8-flickr li a").append("<div class='overlay'><p>Content</p></div>");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM