jQuery-将嵌入式视频移动到顶部?

jQuery-将嵌入式视频移动到顶部?,jquery,Jquery,下面的代码将查找图像(如果存在),并将其移动到标题和文本上方。因此,如果标记如下所示: <h2>Some fancy title</h2> <p>Some interesting text</p> <img src="someimage.jpg" /> <img src="someimage.jpg" /> <h2>Some fancy title</h2> <p>Some intere

下面的代码将查找图像(如果存在),并将其移动到标题和文本上方。因此,如果标记如下所示:

<h2>Some fancy title</h2>
<p>Some interesting text</p>
<img src="someimage.jpg" />
<img src="someimage.jpg" />
<h2>Some fancy title</h2>
<p>Some interesting text 
甚至这个:

<p><h2>Some fancy title</h2></p>
<img src="someimage.jpg" />
<p>Some interesting text</p>
它将重新定位元素,使其看起来像这样:

<h2>Some fancy title</h2>
<p>Some interesting text</p>
<img src="someimage.jpg" />
<img src="someimage.jpg" />
<h2>Some fancy title</h2>
<p>Some interesting text 
现在,我正试图找出如何重新编写代码,这样,如果内容中有视频,无论帖子是否也有图像,它都会将视频移到其他所有内容之上。有人能给我建议或告诉我一个方法吗

这是我现在掌握的当前代码:

$j('#hp-featured-item > div[id^="post-"]').each(function() {
  if($j(this).find('embed')) {
  var id=this.id.match(/^post-([0-9]+)$/);
  var imgt = $j("img:eq(0)");
  var pt = $j("p:not(:has(img)):eq(0)");
  var hh = $j("h2:eq(0)");
  $j(this).html('');
  $j(this).append(imgt).append(hh).append(pt);
  $j(this).each(function() {
   var img = $j('img');
   $j(img).wrap($j('<a>').attr('href', '/blog/?p='+id[1]));
   var h2 = $j('h2');
   $j(h2).wrap($j('<a>').attr('href', '/blog/?p='+id[1]));
  });
  } else {
   var id=this.id.match(/^post-([0-9]+)$/);
   var imgt = $j("embed:eq(0)");
   var pt = $j("p:not(:has(embed)):eq(0)");
   var hh = $j("h2:eq(0)");
   $j(this).html('');
   $j(this).append(imgt).append(hh).append(pt);
   $j(this).each(function() {
    var img = $j('embed');
    $j(img).wrap($j('<a>').attr('href', '/blog/?p='+id[1]));
    var h2 = $j('h2');
    $j(h2).wrap($j('<a>').attr('href', '/blog/?p='+id[1]));
   });
  }
 });

使用jQuery,您要做的事情非常简单。我把一个测试页面和一个建议的解决方案放在一起。在重新排列测试页面之前,请确保查看测试页面的源代码以查看原始HTML内容

您可以在此处查看测试页面:

以下是jQuery代码的返工:

$j('#hp-featured-item > div[id^="post-"]').each(function() {
    var $div = $j(this),
        $h2  = $div.find('h2:first'),
        $obj = $div.find('object, embed, img').filter(':first'),
        id   = this.id.match(/^post-([0-9]+)$/);

    if( $obj.size() > 0){
        // Find parent
        var $par = $obj.closest('p');

        // Move to top of div
        $obj.prependTo($div);

        // Remove the now empty parent
        $par.remove();

        if( $obj.is('img')){
            // You can't wrap objects and embeds with links, so make sure we just wrap images
            $obj.wrap( $j('<a></a>').attr('href', '/blog/?p='+id[1]));
        }
    }

    // Wrap the contents of the h2, not the h2 itself, with a link
    $h2.wrapInner( $j('<a></a>').attr('href', '/blog/?p='+id[1]) );

}); 

你能进一步解释一下你想做什么吗?您当前的代码看起来像是清除了帖子中的所有内容,除了第一个h2、第一个img和第一段不包含图像的内容。然后,它将图像包装在一个链接中,并根据需要将其放在h2或段落的前面。这也是你对嵌入标签的期望结果吗?这太棒了!比我想做的好多了。。。谢谢