重新运行自定义Modernizr测试

re-run custom Modernizr test

本文关键字:测试 Modernizr 自定义 重新运行      更新时间:2023-09-26

我有一个测试,它将根据特定的用户交互进行更改。我正试图把它与现代化联系起来。

我试图通过再次调用addTest来强制重新运行测试。

//this works the first time
//adds html.mousing class
Modernizr.addTest('mousing',function(){
    return true;
});

//later in the code
//this DOES NOT add html.no-mousing
Modernizr.addTest('mousing',function(){
    return false;
});

有什么想法可以重新运行或刷新现代化测试吗?

Modernizr用于测试浏览器功能,而不是根据用户交互任意切换类。(仅仅因为用户与您的页面进行了交互并不意味着浏览器突然无法"鼠标")。您最好不要将其添加为Modernizr测试,而只需在需要时添加/删除类名。

然而,要回答您的问题,您需要删除Modernizr上mousing的缓存值并重新运行测试,同时手动删除html元素上的任何匹配类名。类似于:

Modernizr.addTest('mousing',function(){
  return true;
});
/* ... */
delete Modernizr.mousing;
Modernizr.addTest('mousing',function(){
  document.html.className = document.html.className.replace(/'b(no-)?mousing'b/g, '');
  return false;
});