且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查文本是否在网页中具有粗体

更新时间:2023-12-04 22:03:28

window.find() ,在支持的情况下,将选择移动到其找到的文本。在这种情况下,您可以使用 document.queryCommandState()来检查所选内容的粗体,无论粗​​体设置如何,CSS都具有工作优势(CSS font-weight < b> < strong> 元素)。不幸的是,在非IE浏览器中,您必须暂时将文档置于设计模式,以使其工作:

window.find(), where supported, moves the selection to the text it has found. This being the case, you can use document.queryCommandState() to check the boldness of the selected content, which has the advantage of working regardless of how the boldness is set (CSS font-weight, <b> or <strong> elements). Unfortunately in non-IE browsers you have to temporarily put the document into design mode to make this work:

演示: http://jsfiddle.net/efN8P/

代码:

document.designMode = "on";
window.find("I am");
var isAllBold = document.queryCommandState("Bold");
alert(isAllBold);
document.designMode = "off";