简体   繁体   English

使用jquery在父标签之外附加html

[英]append html outside of the parent tag using jquery

So I have this scenario 所以我有这种情况

<div id="editor" contenteditable="true">
    <div id="list">Hello</div>
</div>

I want to call 我想打电话

var html = $('<div id="replaced" contenteditable="false">Hello</div>');
$('#list').replaceWith(html);

The problem is I want the output to be like this: 问题是我希望输出是这样的:

<div id="editor" contenteditable="true">
    <div id="replaced" contenteditable="false">Hello</div><br />
</div>

I tried the following and it didn't work: (Where this is the div replacing tmp.mention) 我尝试了以下操作,但没有成功:(这是替换tmp.mention的div)

$('#'+tmp.mention).replaceWith(this).parent().after('<br />');

I want to do this because the div replacing tmp.mention has contenteditable set to false inside a div editor that has a contenteditable set to true. 我想这样做是因为替换tmp.mention的div在将contenteditable设置为true的div编辑器中将contenteditable设置为false。 If there's no 如果没有
the user will not be able to type after the div... 用户将无法在div之后键入...

Try this: 尝试这个:

$('#'+tmp.mention).replaceWith($(this).parent()).after('<br />');

I think you need 我觉得你需要

var html = $('<div id="replaced" contenteditable=false>Hello</div>');
$('#list').replaceWith(html);

DEMO 演示

But if you want to whole #editor then 但是,如果您想整个#editor

var html = $('<div id="replaced" contenteditable=false>Hello</div>');
$('#list').parent().replaceWith(html);

DEMO 演示

According to edit 根据编辑

If you want output like 如果你想输出像

<div id="editor" contenteditable="true">
    <div id="replaced" contenteditable="false">Hello</div><br />
</div

then try 然后尝试

var html = $('<div id="replaced" contenteditable=false>Hello</div>');
$('#list').replaceWith(html).after('</br>');

尝试这个:

$('#list').replaceWith('<div id="replaced" contenteditable="false">Hello</div><br />')

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

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