[英]JSLint in Brackets says Audio is not defined, but doesn't complain about Image
I have this code: 我有以下代码:
var me = {
PreloadImage: function(src) {
var e = new Image();
e.src = src;
},
CreateAudio : function(src) {
var c = new Audio(src);
c.play();
}
}
I have a problem with new Audio()
, as JSLint
in Brackets says that Audio
has not been defined, but it doesn't say the same for Image
: 我对new Audio()
有问题,因为JSLint
中的JSLint
表示尚未定义Audio
,但是Image
却没有这样说:
Sometimes I have to do things like window.console.log
instead of console.log
because console.log
was not defined, but if that's the case, what do I have to add before Audio
and Image
? 有时我必须做诸如window.console.log
之类的事情而不是console.log
类的事情,因为未定义console.log
,但是如果是这样的话,在Audio
和Image
之前我必须添加什么?
Instead of var c = new Audio(src);
代替var c = new Audio(src);
use var c = document.createElement('audio'); c.src=src; c.play();
使用var c = document.createElement('audio'); c.src=src; c.play();
var c = document.createElement('audio'); c.src=src; c.play();
The accepted answer doesn't look like the best way to address that issue to me. 接受的答案似乎不是解决我这个问题的最佳方法。 You should either change your JSLint
options or disable that rule on that specific line. 您应该更改JSLint
选项或在该特定行上禁用该规则。
In both JSLint
and ESLint
, you can fix it either adding it as a global: 在JSLint
和ESLint
,都可以修复它,也可以将其添加为全局ESLint
:
{
"globals": {
"Audio": true
}
}
{
"env": {
"browser": true
}
}
JSLint:
{
"browser": true
}
You can also update them in Brackets' preferences: 您也可以在Brackets的偏好设置中更新它们:
"jslint.options": {
"browser": true,
OR
"globals": {
"Audio": true
}
}
You can also disable linting for that line or for that specific rule on that line: 您还可以为该行或该行上的特定规则禁用掉毛:
ESLint
: ESLint
:
const c = new Audio(src); // eslint-disable-line
Or: 要么:
// eslint-disable-next-line
const c = new Audio(src);
JSLint
: JSLint
:
const c = new Audio(src); // jslint ignore:line
You have to wait for the DOM to be ready. 您必须等待DOM准备就绪。 Since your are using jQuery, please encapsulate your code in that: 由于您使用的是jQuery,因此请在其中封装代码:
$(document).ready(function () {
// Your code...
});
You can also use this syntax: 您还可以使用以下语法:
$(function () {
// Your code...
});
(Bonus tip: use the switch
instruction in your code. RoNBeta.js
is a bit scary...) (奖金提示:在代码中使用switch
指令RoNBeta.js
有点吓人...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.