Javascript Jquery-如何在div中的文本之前添加elemend?

Javascript Jquery-如何在div中的文本之前添加elemend?,javascript,jquery,Javascript,Jquery,我在Jquery中的代码有问题。我需要在div中的文本之前放置一个元素,但它保持在同一位置。你能帮助我吗?多谢各位 Jquery: $('.content_wrap img').each(function(){ if($(this).nextAll('.darkblue').length !== 0){ $('.darkblue').next('<div>').prepend($(this)); } }); $('.content\u wrap img')。

我在Jquery中的代码有问题。我需要在div中的文本之前放置一个元素,但它保持在同一位置。你能帮助我吗?多谢各位

Jquery:

$('.content_wrap img').each(function(){
   if($(this).nextAll('.darkblue').length !== 0){
      $('.darkblue').next('<div>').prepend($(this));
   }
});
$('.content\u wrap img')。每个(函数(){
if($(this).nextAll('.darkblue').length!==0){
$('.darkblue').next('').prepend($(this));
}
});
HTML:


软件维护以固定的速率进行分组。
为什么不试试呢

$("div").prepend($this)

如果这是文件中唯一的div。

您需要找到
nextblue
(the
h2
)的父级,然后找到下一个div:

$('.content_wrap img').each(function(){
   if($(this).nextAll('.darkblue').length !== 0){
      $('.darkblue').closest("h2").next('div').prepend($(this));
   }
});
$('.content_wrap img').each(function(){
   if($(this).nextAll('.darkblue').length !== 0){
       $('.darkblue').parent().next().prepend($(this));
   }
});

你需要找到父母的下一个div,而不是下一个div:

$('.content_wrap img').each(function(){
   if($(this).nextAll('.darkblue').length !== 0){
      $('.darkblue').closest("h2").next('div').prepend($(this));
   }
});
$('.content_wrap img').each(function(){
   if($(this).nextAll('.darkblue').length !== 0){
       $('.darkblue').parent().next().prepend($(this));
   }
});
试一试

演示:


如果要检查下一个元素是否具有类darkblue,则

$('.content_wrap img').each(function () {
    var $this = $(this);
    if ($this.next().hasClass('darkblue')) {
        $this.prependTo($this.parent().next())
    }
});
演示:

工作解决方案:

必须在

软件维护以固定的速率进行分组。
试试这个

$('.darkblue').parent("h2").next("div").prepend($(this));

那就给他一个具体的身份证,比如。。。。。。然后使用以下格式:$(“#addcontent”).prepend($this)这将使您的生活更加轻松。您缺少检查是否存在
darkblue
链接
$('.darkblue').parent("h2").next("div").prepend($(this));