简体   繁体   English

JavaScript:无法使Modernizr工作

[英]JavaScript: Can't get Modernizr working

I've been trying to get Modernizr working, but I think I must be doing something wrong, as I can't seem to get it doing anything at all. 我一直在尝试使Modernizr正常运行,但是我认为我一定做错了事,因为我似乎根本无法做任何事情。

My ultimate purpose is to try and test browser support for webkit-line-clamp , but I've just been trying to get an example from the doc's working, and I don't even seem able to do that. 我的最终目的是尝试测试浏览器对webkit-line-clamp ,但是我一直在尝试从文档的工作中获取一个示例,但我似乎还没有做到这一点。

So, I went to the Modernizr website, and selected the Modernizr.addtest() code, and copied it into the head of my html page, as so: 因此,我去了Modernizr网站,选择了Modernizr.addtest()代码,然后将其复制到html页面的开头,如下所示:

<script>
/*! modernizr 3.5.0 (Custom Build) | MIT *
 * https://modernizr.com/download/?-addtest-setclasses !*/
!function(n,e,o){function t(n,e){return typeof n===e}function s(){var n,e,o,s,i,a,l;for(var u in f)if(f.hasOwnProperty(u)){if(n=[],e=f[u],e.name&&(n.push(e.name.toLowerCase()),e.options&&e.options.aliases&&e.options.aliases.length))for(o=0;o<e.options.aliases.length;o++)n.push(e.options.aliases[o].toLowerCase());for(s=t(e.fn,"function")?e.fn():e.fn,i=0;i<n.length;i++)a=n[i],l=a.split("."),1===l.length?Modernizr[l[0]]=s:(!Modernizr[l[0]]||Modernizr[l[0]]instanceof Boolean||(Modernizr[l[0]]=new Boolean(Modernizr[l[0]])),Modernizr[l[0]][l[1]]=s),r.push((s?"":"no-")+l.join("-"))}}function i(n){var e=c.className,o=Modernizr._config.classPrefix||"";if(d&&(e=e.baseVal),Modernizr._config.enableJSClass){var t=new RegExp("(^|\\s)"+o+"no-js(\\s|$)");e=e.replace(t,"$1"+o+"js$2")}Modernizr._config.enableClasses&&(e+=" "+o+n.join(" "+o),d?c.className.baseVal=e:c.className=e)}function a(n,e){if("object"==typeof n)for(var o in n)u(n,o)&&a(o,n[o]);else{n=n.toLowerCase();var t=n.split("."),s=Modernizr[t[0]];if(2==t.length&&(s=s[t[1]]),"undefined"!=typeof s)return Modernizr;e="function"==typeof e?e():e,1==t.length?Modernizr[t[0]]=e:(!Modernizr[t[0]]||Modernizr[t[0]]instanceof Boolean||(Modernizr[t[0]]=new Boolean(Modernizr[t[0]])),Modernizr[t[0]][t[1]]=e),i([(e&&0!=e?"":"no-")+t.join("-")]),Modernizr._trigger(n,e)}return Modernizr}var r=[],f=[],l={_version:"3.5.0",_config:{classPrefix:"",enableClasses:!0,enableJSClass:!0,usePrefixes:!0},_q:[],on:function(n,e){var o=this;setTimeout(function(){e(o[n])},0)},addTest:function(n,e,o){f.push({name:n,fn:e,options:o})},addAsyncTest:function(n){f.push({name:null,fn:n})}},Modernizr=function(){};Modernizr.prototype=l,Modernizr=new Modernizr;var u;!function(){var n={}.hasOwnProperty;u=t(n,"undefined")||t(n.call,"undefined")?function(n,e){return e in n&&t(n.constructor.prototype[e],"undefined")}:function(e,o){return n.call(e,o)}}();var c=e.documentElement,d="svg"===c.nodeName.toLowerCase();l._l={},l.on=function(n,e){this._l[n]||(this._l[n]=[]),this._l[n].push(e),Modernizr.hasOwnProperty(n)&&setTimeout(function(){Modernizr._trigger(n,Modernizr[n])},0)},l._trigger=function(n,e){if(this._l[n]){var o=this._l[n];setTimeout(function(){var n,t;for(n=0;n<o.length;n++)(t=o[n])(e)},0),delete this._l[n]}},Modernizr._q.push(function(){l.addTest=a}),s(),i(r),delete l.addTest,delete l.addAsyncTest;for(var p=0;p<Modernizr._q.length;p++)Modernizr._q[p]();n.Modernizr=Modernizr}(window,document);
</script>

and then added the following code (as per the example at https://modernizr.com/docs ): 然后添加以下代码(按照https://modernizr.com/docs上的示例):

<script>
Modernizr.addTest('itsTuesday', function() {
var d = new Date();
return d.getDay() === 2;
});
</script>

and then, at the foot of the page, added the following code: 然后在页面底部添加以下代码:

<script>
if (itstuesday())
{
alert("it's Tuesday");
}
else
{
alert("Not Tuesday");
}
</script>

I also tested with if (Modernizr.itsTuesday()) and if (itsTuesday()) , and if (Modernizr.itstuesday()) and everything else I could think of that might work, but all I end up with in the console are errors such as Uncaught ReferenceError: itstuesday is not defined and Uncaught ReferenceError: Modernizer is not defined , etc. 我还测试了if (Modernizr.itsTuesday())if (itsTuesday())以及if (Modernizr.itstuesday())以及我能想到的其他所有功能,但最终在控制台中找到的都是错误,例如Uncaught ReferenceError: itstuesday is not definedUncaught ReferenceError: itstuesday is not defined Uncaught ReferenceError: Modernizer is not defined等。

The Modernizr doc's do note " One thing to notice is that the names of feature detect functions are always lowercased when added to the Modernizr object. That means that Modernizr.itsTuesday will not exist, but Modernizr.itstuesday will. " “ Modernizr doc的确注意到” 注意的一件事是,特征检测函数的名称在添加到Modernizr对象时总是小写。这意味着Modernizr.itsTuesday将不存在,而Modernizr.itstuesday将存在。

I guess I'm just not understanding how I'm meant to be using or implementing the code properly, so any assistance would be much appreciated. 我想我只是不了解我应该如何正确使用或实现代码,因此,我们将不胜感激。

If, further to that, anyone can suggest exactly how I can go about testing feature support, such as webkit-line-clamp , with Modernizr then even better. 如果除此之外,任何人都可以建议我如何使用Modernizr测试功能支持,例如webkit-line-clamp ,那就更好了。 Unfortunately, although the site has a ton of things one can test for, with their default set of code, that particular property doesn't seem to be amongst them. 不幸的是,尽管该网站具有大量可以测试的功能,但使用其默认的代码集,该特定属性似乎并不在其中。

Thanks! 谢谢!

Update: 更新:

Okay, so I managed to get this working. 好吧,所以我设法解决了这个问题。 I needed to use the Modernizr.addTest() and the Modernizr.testAllProps() , and then added the test: 我需要使用Modernizr.addTest()Modernizr.testAllProps() ,然后添加测试:

<script>
Modernizr.addTest('lineclamp', function() {
    return Modernizr.testAllProps('line-clamp');
});
</script>

and made use of it per below: 并按以下方式使用它:

<script>
if (Modernizr.lineclamp)
{
alert("yay");
}
else
{
alert("nay");
}
</script>

Tested it in several different browsers and got the anticipated results. 在几种不同的浏览器中对其进行了测试,并获得了预期的结果。

From my reading of the docs, you'd do this instead: 从我阅读的文档中,您可以这样做:

if(Modernizr.itsTuesday) {
  // do something
} else {
  // something else
}

Tests appear to result in properties, not functions. 测试似乎会导致属性而非功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM