如何检查文本是否在网页中具有粗体 [英] How to check whether a text is having bold in a web page

查看:146
本文介绍了如何检查文本是否在网页中具有粗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的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:

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

代码:

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

这篇关于如何检查文本是否在网页中具有粗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆