admin管理员组文章数量:1420279
I am having trouble (plete) with execCommand so you help much appreciated - I have no doubt I am barking up the wrong tree but ..... anyway
I have a div like this
<div class="editable" id="editor" contenteditable="true">
<2>Some text in here</h2> blah blah blah ...
</div>
That is "editable" i.e. document.designMode = 'on';
- it get's this "state" on focus. On blur it changes to document.designMode = 'off';
I have a "test button"
<input type="button" id="bold" value="Bold">
That when "clicked" makes the "highlighted text" bold - hence execCommand
So far I have something like this:
function getSelected() {
if(window.getSelection) { return window.getSelection(); }
else if(document.getSelection) { return document.getSelection(); }
else {
var selection = document.selection && document.selection.createRange();
if(selection.text) { return selection.text; }
return false;
}
return false;
}
$('#bold').click(function(){
var selection = getSelected();
alert(selection);
});
The alert (bold click) does give me the highlighted/selected text but I cannot work out how to "turn it" bold. I guess I need to access the innerHTML or something?
Help much appreciated - thanks in advance. OH and I do not want to use either an i-frame or a textarea
I am having trouble (plete) with execCommand so you help much appreciated - I have no doubt I am barking up the wrong tree but ..... anyway
I have a div like this
<div class="editable" id="editor" contenteditable="true">
<2>Some text in here</h2> blah blah blah ...
</div>
That is "editable" i.e. document.designMode = 'on';
- it get's this "state" on focus. On blur it changes to document.designMode = 'off';
I have a "test button"
<input type="button" id="bold" value="Bold">
That when "clicked" makes the "highlighted text" bold - hence execCommand
So far I have something like this:
function getSelected() {
if(window.getSelection) { return window.getSelection(); }
else if(document.getSelection) { return document.getSelection(); }
else {
var selection = document.selection && document.selection.createRange();
if(selection.text) { return selection.text; }
return false;
}
return false;
}
$('#bold').click(function(){
var selection = getSelected();
alert(selection);
});
The alert (bold click) does give me the highlighted/selected text but I cannot work out how to "turn it" bold. I guess I need to access the innerHTML or something?
Help much appreciated - thanks in advance. OH and I do not want to use either an i-frame or a textarea
Share Improve this question asked Mar 16, 2011 at 13:56 Russell ParrottRussell Parrott 9037 gold badges19 silver badges39 bronze badges1 Answer
Reset to default 7I strongly remend using Rangy for dealing with text selection.
A cross-browser JavaScript range and selection library. It provides a simple standards-based API for performing mon DOM Range and Selection tasks in all major browsers, abstracting away the wildly different implementations of this functionality between Internet Explorer and DOM-pliant browsers.
You can use the CSS Class Applier Module to bold the text (live demo).
本文标签: javascriptJSJQuery execCommand problemStack Overflow
版权声明:本文标题:javascript - JSJQuery execCommand problem - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745333872a2653946.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论