最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Toggling H1 and h2 with execCommand - Stack Overflow

programmeradmin3浏览0评论

I have a simple text editor and i'd like to toggle h1 tags around the current selection as i do with bold tags. With bold tags i do:

function onBoldClick() {
   document.execCommand( 'bold', false );
}

This automatically toggles b tags around the current selection.

With h1 tags:

function onHeading1Click() {
    document.execCommand('formatBlock', false, '<h1>'); 
}

This only wraps h1 around the selection but there's no way to remove it.
Is there another way to go about this? Nb: it should work on i.e.

I have a simple text editor and i'd like to toggle h1 tags around the current selection as i do with bold tags. With bold tags i do:

function onBoldClick() {
   document.execCommand( 'bold', false );
}

This automatically toggles b tags around the current selection.

With h1 tags:

function onHeading1Click() {
    document.execCommand('formatBlock', false, '<h1>'); 
}

This only wraps h1 around the selection but there's no way to remove it.
Is there another way to go about this? Nb: it should work on i.e.

Share Improve this question asked Mar 1, 2014 at 18:32 SkyalchemistSkyalchemist 4611 gold badge7 silver badges18 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9
var h1Class = 'your h1 btn class',
    removeH1Class = '.remove-h1';

$(document).on('click', h1Class, function() {
    $(this).removeClass(h1Class).addClass(removeH1Class);
    onHeading1Click();
});

$(document).on('click', removeH1Class, function() {
    $(this).removeClass(removeH1Class).addClass(h1Class);
    onRemoveH1Click();
});

function onHeading1Click() {
    document.execCommand('formatBlock', false, '<h1>'); 
}
function onRemoveH1Click() {
    document.execCommand('formatBlock', false, 'p'); 
}

HTML

 <p><button type="button" id="h1-button">H1</button></p>
 <div id="edit-area" contenteditable="true">
     <h1>This is heading</h1>
     <div>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed posuere interdum sem. Quisque ligula eros ullamcorper quis, lacinia quis facilisis sed sapien. Mauris varius diam vitae arcu. Sed arcu lectus auctor vitae, consectetuer et venenatis eget velit. Sed augue orci, lacinia eu tincidunt et eleifend nec lacus. Donec ultricies nisl ut felis, suspendisse potenti.</div>
 </div>

Javascript + jQuery

$('#h1-button').click(function() {
    var html = $('#edit-area').html();
    document.execCommand('formatBlock', false, 'h1');
    if(html == $('#edit-area').html()) {
        document.execCommand('formatBlock', false, 'div');
    }
});

JSFIDDLE DEMO

This is my choice to do this. It's CSS hack and broke semantic, but it's works.

HTML

<p><button type="button" id="h1-button">H1</button></p>
<div contenteditable="true">
    <sup>This is heading</sup>
    <div>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed posuere interdum sem. Quisque ligula eros ullamcorper quis, lacinia quis facilisis sed sapien.</div>
</div>

Javascript + jQuery

$('#h1-button').click(function() {
    document.execCommand('superscript', false, null);
});

CSS

sup{
    vertical-align: super;
    display: block;
    font-size: 2em;
    line-height: 1em;
    margin: 20px 0;
    position: relative;
    top: 0;
}

JSFIDDLE DEMO

发布评论

评论列表(0)

  1. 暂无评论