简体   繁体   中英

execCommand - wrap content including tags in blockquote tags

I am trying to wrap some selected elements in <blockquote> tags but the method I thought might work replaces the existing tags rather than wrapping them.

Here's my code.

$("input[value='Quote']").on("click", function() {
    document.execCommand('formatBlock', false, '<blockquote>');
});

and...

<div contentEditable>
    <p>para 1</p>
    <p>para 2</p>
</div>

<input type="button" value="Quote" />

I want to end up with something like this...

<div contentEditable>
    <blockquote>
        <p>para 1</p>
        <p>para 2</p>
    </blockquote>
</div>

rather than the following which is what I currently get...

<div contentEditable>
    <blockquote>
        para 1
        <br />
        para 2
    </blockquote>
</div>

Thanks

This should do it

$("input[value='Quote']").on("click", function() {
  $("<blockquote/>").insertBefore($("[contenteditable]").find("p:first")).append($("[contenteditable]").find("p"))
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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