繁体   English   中英

在div中找到第一个img,然后在其下方放置一个新的div

[英]Find the first img inside a div and place a new div below it

我正在尝试使用下面的代码在jQuery的图像下方放置一个div,但是它不起作用。

<head>我有以下代码:

<script type="text/javascript">
$( document ).ready(function() {
$(".gdl-blog-full").find("img").prepend("<div id='1' style='width:400px; float: left; margin-top:10px; background-color: #000;'> You are watching 5th object out of 100 </div>");
});
</script>

<body>里面,我有这个:

<div class="gdl-blog-full">
<img src="logoF.png" width="400" height="93" />
</div>

我只想在图像下方放置一个<div>

这样

    $('.gdl-blog-full>img:first-child').after("<div>hello</div>");

使用$ .after

$('.gdl-blog-full').find('img:first-child').after('<div id='1' style='width:400px; float: left; margin-top:10px; background-color: #000;'> You are watching 5th object out of 100 </div>');

使用.after将元素放置在指定元素之后,也使用:first-child获得第一个图像:

$(".gdl-blog-full").find("img:first-child").after("<div id='1' style='width:400px; float: left; margin-top:10px; background-color: #000;'> You are watching 5th object out of 100 </div>");

不应该在之后

<script type="text/javascript">
$( document ).ready(function() {
$(".gdl-blog-full").find("img").after("<div id='1' style='width:400px; float: left; margin-top:10px; background-color: #000;'> You are watching 5th object out of 100 </div>");
});
</script>

使用.insertAfter将元素放在另一个之后。

$("<div id='1' style='width:400px; float: left; margin-top:10px; background-color: #000;'> You are watching 5th object out of 100 </div>").insertAfter(".gdl-blog-full img:first-child");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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