Replace HR Element with Open Div

这一生的挚爱 提交于 2020-01-04 06:05:09

问题


So I have been stumped with this for a while now and can't really seem to get around it. I have some content that is added through a wysiwyg editor. What I want to do is use the <hr> element to add divs in there to control the look of the container. The problem is that it adds the closing div. I want to add the closing div to the next hr element.

var firstChild = jQuery('.content-container-wrapper hr:first-child');
var lastChild = jQuery('.content-container-wrapper hr:last-child');
if (firstChild) {
    jQuery(firstChild).replaceWith('<div class="working">');
};

Is there a way to tell the browser not to add the closing div?


回答1:


It sounds like you want to wrap everything starting with the first HR and ending with last.

Following will wrap everything between the two HR and then remove them( my interpretation of your question).

var $start=jQuery('.content-container-wrapper hr:first-child'),
    $end=jQuery('.content-container-wrapper hr:last-child');

$start.nextUntil( $end).wrapAll( '<div class="working">');
$start.add($end).remove();

You can't insert part of an element and then insert the end the way you do in a text editor

API reference:

http://api.jquery.com/wrapAll/

http://api.jquery.com/nextUntil/



来源:https://stackoverflow.com/questions/15192145/replace-hr-element-with-open-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!