javascript 检测选项标记文本缩进功能或检测浏览器类型作为后备
我有一个选择下拉列表,它应该代表站点中页面的层次结构。
<select id="parent" name="parent">
<option value="">(none)</option>
<option class="inset0" value="home">home</option>
<option class="inset1" value="aboutpage">aboutpage</option>
<option class="inset2" value="about">about</option>
<option class="inset2" value="staff">staff</option>
<option class="inset1" value="news">news</option>
<option class="inset1" value="products">products</option>
<option class="inset2" value="starter">starter</option>
<option class="inset3" value="nesso">nesso</option>
</select>
该结构是在后端进行的,并使用 CSS 进行样式设置。
option.inset0 {text-indent:0px;}
option.inset1 {text-indent:10px;}
option.inset2 {text-indent:20px;}
option.inset3 {text-indent:30px;}
option.inset4 {text-indent:40px;}
option.inset5 {text-indent:50px;}
option.inset6 {text-indent:60px;}
这使得更深级别的选项根据每个页面在站点层次结构中的深度显示为缩进。然而,这种样式仅适用于 FireFox,不适用于 Chrome。我知道浏览器和操作系统之间的控件样式很大程度上不一致,所以我不会去那里。相反,我想在每个选项的文本之前添加“-”字符,以使层次结构可见。我只需要对 Chrome 执行此操作,因为我在这个项目中根本不支持 IE。
对象文本的操作将在 JavaScript 中完成。
我的问题是这样的:
知道不建议使用浏览器检测而倾向于使用功能检测,那么如何针对此浏览器行为执行功能检测?如果这是不可能的,是否有一个快速测试可以让我知道我是在 Chrome/webkit 还是 Firefox/Gecko 中?
I have a select drop-down that is supposed to represent the hierarchy of pages in a site.
<select id="parent" name="parent">
<option value="">(none)</option>
<option class="inset0" value="home">home</option>
<option class="inset1" value="aboutpage">aboutpage</option>
<option class="inset2" value="about">about</option>
<option class="inset2" value="staff">staff</option>
<option class="inset1" value="news">news</option>
<option class="inset1" value="products">products</option>
<option class="inset2" value="starter">starter</option>
<option class="inset3" value="nesso">nesso</option>
</select>
The construction is made on the backend and styled using css.
option.inset0 {text-indent:0px;}
option.inset1 {text-indent:10px;}
option.inset2 {text-indent:20px;}
option.inset3 {text-indent:30px;}
option.inset4 {text-indent:40px;}
option.inset5 {text-indent:50px;}
option.inset6 {text-indent:60px;}
This is making options of deeper levels display as indented according to the depth each page is in the site hierarchy. However this styling only works in FireFox, and not in Chrome. I know that styling of controls is largely uneven among browsers and operating systems, so I will not go there. Instead I would like to add "-" characters before the text of each option in order to make the hierarchy visible. I only need to do this for Chrome, since I am not supporting IE at all in this project.
The manipulation of the object's text will be done in javascript.
My question is this:
Knowing that browser detection is not recommended and feature detection is favored instead, how can I perform a feature detection for this browser behavior? If this is not possible, is there a quick test that will allow me to know if I'm in Chrome/webkit or Firefox/Gecko?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法想象人们如何检测到这一点。我想也许计算的样式可能没有设置,但快速测试表明它是。
有一个官方页面(http://trac.webkit.org/wiki/DetectingWebKit)关于检测 WebKit,但它已经有 4 年历史了,脚本只是检查用户代理字符串,这基本上与 jQuery 对 $.browser.webkit 所做的事情相同。
至于专门检测Chrome,它定义了
window.chrome
(我找不到官方文档,但请参阅使用 Javascript 检测 Google Chrome 的基于安全功能的方法?),但我猜渲染问题将出现在所有 Webkit 浏览器中。I can't imagine how one would feature detect this. I thought maybe the computed style may not be set, however a quick test showed it is.
There is an offical page (http://trac.webkit.org/wiki/DetectingWebKit) about detecting WebKit, but it's 4 years old, and the script simply checks the User Agent string, which is basically the same thing jQuery does for
$.browser.webkit
.As for specifically detecting Chrome, it defines
window.chrome
(I can't find an official documentation, but see Safe feature-based way for detecting Google Chrome with Javascript?), but I guess the rendering problem will be in all Webkit browsers.