我想根据元素使用的浏览器向元素添加一个类。这就是我所拥有的:
var browser = navigator.userAgent;
if (browser.match("Firefox")){
$(".dollar").addClass("ff");
}
但似乎不起作用。我做错了什么?
编辑:HTML
<p class="dollar">$</p>
最佳答案
使用文档(DOM)就绪包装器:
var browser = navigator.userAgent;
jQuery(function( $ ){ // DOM is now ready
if (browser.match("Firefox")){
$(".dollar").addClass("ff"); // and .dollar is parsed
}
});
测试这个 jsBin demo Firefox 中的示例
关于javascript - 使用 Javascript 将类添加到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35756618/