JQuery append() DIV HTML 不带

发布于 2024-10-18 23:35:49 字数 1096 浏览 2 评论 0原文

一直试图找到这个问题的答案,但似乎无法理解它:

var newLayout = '<div id="blog-left"></div>' ; // This creates the layout the content will be moved to
    newLayout += '<div id="blog-right" class="nivoSlider"></div>' ;
    newLayout += '<div style="clear:both;"></div>' ;
    newLayout += '<div id="img-temp" style="display:none;"></div>' ;

$('.blog').append(newLayout); // Add the new layout

$('#blog-left').append( $('.blog p') ); // All <p>'s being added to the new layout
$('#blog-right').append( $('.blog img') ); // All <IMG>'s being added to the new layout

这工作得很好,但是我遇到的问题是而不是将

附加到 #blog-left 我想附加所有包含的 HTML 但减去 标签。因此,所有 HTML 都进入 #blog-left,而 进入 #blog-right。

我尝试过使用 .html() ,它在一定程度上有效,但 #blog-left & #blog-right 位于包含 div .blog 中。

我已经尝试了一些方法,但没有任何结果正确返回(如果有的话)。那么各位聪明人有使用 JQuery 的解决方案吗?

提前致谢。

萨姆·T。

Been trying to find an answer to this but can't seem to get my head around it:

var newLayout = '<div id="blog-left"></div>' ; // This creates the layout the content will be moved to
    newLayout += '<div id="blog-right" class="nivoSlider"></div>' ;
    newLayout += '<div style="clear:both;"></div>' ;
    newLayout += '<div id="img-temp" style="display:none;"></div>' ;

$('.blog').append(newLayout); // Add the new layout

$('#blog-left').append( $('.blog p') ); // All <p>'s being added to the new layout
$('#blog-right').append( $('.blog img') ); // All <IMG>'s being added to the new layout

This work really well, however the problem I have is instead of appending the <p> to #blog-left I want to append all the contained HTML but minus the <IMG> tags. So all HTML goes into #blog-left and <IMG>'s go into #blog-right.

I've tried using .html() which works to a point but the #blog-left & #blog-right are in the containing div .blog.

I've tried a couple of things but nothing returns correctly if at all. So do any of you bright sparks have the solution using JQuery?

Thanks in advance.

Sam T.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺ぐ爱和你が 2024-10-25 23:35:49

我会这样做:

var $blog = $('.blog');

// first add all images to right (removes them from .blog)
var $right = $('<div id="blog-right" class="nivoSlider" />')
              .append($blog.find('img'));

// add all remaining elements from .blog to left
var $left = $('<div id="blog-left" />').append($blog.children());

var container =  '<div style="clear:both;"></div>' + 
                 '<div id="img-temp" style="display:none;"></div>';

// add the new content to .blog
$blog.append($left).append($right).append(container);

I would do it like so:

var $blog = $('.blog');

// first add all images to right (removes them from .blog)
var $right = $('<div id="blog-right" class="nivoSlider" />')
              .append($blog.find('img'));

// add all remaining elements from .blog to left
var $left = $('<div id="blog-left" />').append($blog.children());

var container =  '<div style="clear:both;"></div>' + 
                 '<div id="img-temp" style="display:none;"></div>';

// add the new content to .blog
$blog.append($left).append($right).append(container);
始终不够 2024-10-25 23:35:49

尝试这个来过滤掉 img 标签。

var $blog = $('.blog');
$blog.append(newLayout);
var $p = $blog.find("p");
var $img = $blog.find("img");
$('#blog-left').append($p);
$('#blog-right').append($img);

jsfiddle 上的示例。

Try this to filter out img tags.

var $blog = $('.blog');
$blog.append(newLayout);
var $p = $blog.find("p");
var $img = $blog.find("img");
$('#blog-left').append($p);
$('#blog-right').append($img);

Example on jsfiddle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文