确定网页中选定的文本是否为粗体 [英] Identify whether the selected text in a web page is bold nor not
问题描述
我试图确定一个选定的文本(在Firefox中)是否大胆?例如:
< p>某些< b>文字已输入< / b>这里< / p为H.
< p>有些< span style =font-weight:bold>输入了更多文字< / span>这里< / p为H.
用户既可以选择粗体文本的一部分,也可以选择全粗体文本。这是我想要做的:
pre $函数isSelectedBold(){
var r = window.getSelection() .getRangeAt(0);
//然后呢?
$ / code $ / pre
您能帮我吗?
谢谢
Srikanth
如果所选内容位于可编辑元素或文档中,这很简单: 否则,这有点麻烦:在非IE浏览器中,必须暂时使文档可编辑: I am trying to identify whether a selected text (in Firefox) is bold or not? For e.g.: The user can either select a part of bold text, or the full bold text. Here is what I am trying to do: Could you please help me? Thanks If the selection is within an editable element or document, this is simple: Otherwise, it's a little trickier: in non-IE browsers, you'll have to temporarily make the document editable:
这篇关于确定网页中选定的文本是否为粗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
pre $ function selectionIsBold(){
var isBold = false;
if(document.queryCommandState){
isBold = document.queryCommandState(bold);
}
return isBold;
$ b
pre $ 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;
}
<p>Some <b>text is typed</b> here</p>
<p>Some <span style="font-weight: bold">more text is typed</span> here</p>
function isSelectedBold(){
var r = window.getSelection().getRangeAt(0);
// then what?
}
Srikanthfunction selectionIsBold() {
var isBold = false;
if (document.queryCommandState) {
isBold = document.queryCommandState("bold");
}
return isBold;
}
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;
}