Sri*_*tal 4 javascript format range selection bold
我想确定所选文本(在Firefox中)是否为粗体?例如:
<p>Some <b>text is typed</b> here</p>
<p>Some <span style="font-weight: bold">more text is typed</span> here</p>
Run Code Online (Sandbox Code Playgroud)
用户可以选择粗体文本的一部分,也可以选择完整的粗体文本.这是我想要做的:
function isSelectedBold(){
var r = window.getSelection().getRangeAt(0);
// then what?
}
Run Code Online (Sandbox Code Playgroud)
请你帮助我好吗?
谢谢
Srikanth
Tim*_*own 15
如果选择位于可编辑元素或文档中,则这很简单:
function selectionIsBold() {
var isBold = false;
if (document.queryCommandState) {
isBold = document.queryCommandState("bold");
}
return isBold;
}
Run Code Online (Sandbox Code Playgroud)
否则,它有点棘手:在非IE浏览器中,您必须暂时使文档可编辑:
function selectionIsBold() {
var range, isBold = false;
if (window.getSelection) {
var sel = window.getSelection();
if (sel && sel.getRangeAt && sel.rangeCount) {
range = sel.getRangeAt(0);
document.designMode = "on";
sel.removeAllRanges();
sel.addRange(range);
}
}
if (document.queryCommandState) {
isBold = document.queryCommandState("bold");
}
if (document.designMode == "on") {
document.designMode = "off";
}
return isBold;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3205 次 |
| 最近记录: |