如果我的html页面有很多文本和一些有格式的单词。如果我使用
If my html page is having lot of text and some words having formatting. If I use
windows.find(text);这将有助于在网页中查找文本。但是如何知道文档中的搜索文本是粗体?
It will help to find text in a web page. But how can I know the searched text is in bold in the document?
例如,如果我的页面HTML是:
For example if my page HTML is:
<body> Hello Techie, <b>I</b> am in bold. </body>如果我使用 window.find(我是)它会匹配文本。但是我怎么知道我是是否是粗体呢?
If I use window.find("I am") it will match the text. But how can I know that whether "I am" is in bold or not?
我希望问题清楚。请让我知道,如果你想要进一步的输入是有帮助的。
I hope question is clear. Please let me know if you want any further input to be helpful.
推荐答案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:
演示: jsfiddle/efN8P/
代码:
document.designMode = "on"; window.find("I am"); var isAllBold = document.queryCommandState("Bold"); alert(isAllBold); document.designMode = "off";更多推荐
如何检查文本是否在网页中具有粗体
发布评论