[英]Javascript doesn't work on Chrome and Firefox, but works on IE
I have this line of code that works on IE 我有这行代码可在IE上使用
Javascript: Javascript:
function getInfo() {
var date = new Date(date);
var month = date.getMonth();
var test = "print";
document.getElementById('lol').value = month;
}
Body: 身体:
<input type=text name=lol>
<input type=button onClick=getInfo() value=lol>
It works on IE, but not on Chrome and Firefox. 它适用于IE,但不适用于Chrome和Firefox。 Does Chrome and Firefox has a different Javascript?
Chrome和Firefox是否具有不同的Javascript?
getElementById
is for looking up elements by id
. getElementById
用于通过id
查找元素。 Your input doesn't have an id
, it just has a name
. 您的输入没有
id
,只有一个name
。 Your code works on IE because (some versions of) IE are confused and return elements with name
attributes from getElementById
. 您的代码可在IE上使用,因为IE(某些版本) 感到困惑,并从
getElementById
返回具有name
属性的元素。 This is a bug in IE. 这是IE中的错误。
To correct the problem, either add id="lol"
to your input: 要解决此问题,请在您的输入中添加
id="lol"
:
<input type=text name=lol id=lol>
<input type=button onClick=getInfo() value=lol>
...or use querySelector("[name=lol]")
: ...或使用
querySelector("[name=lol]")
:
document.querySelector('[name=lol]').value = month;
querySelector
returns the first element it can find that matches the given CSS selector. querySelector
返回它可以找到的与给定CSS选择器匹配的第一个元素。 It's available on all modern browsers, and IE8. 它在所有现代浏览器和IE8上都可用。
Separately: Your code is passing undefined
into the Date
constructor, here: 分别:您的代码
undefined
传递给Date
构造函数,这里:
var date = new Date(date);
...because that code is, in effect: ...因为该代码实际上是:
var date;
date = new Date(date);
...and variables start out with the value undefined
. ...并且变量从值
undefined
。
On a browser with a standards-compliant JavaScript engine, you'll end up with an invalid date, because the specification requires that the engine convert the argument to a number and then use that as the underyling "time value" (milliseconds-since-the-epoch). 在使用符合标准的JavaScript引擎的浏览器上,您将得到一个无效的日期,因为规范要求引擎将参数转换为数字,然后将其用作底线“时间值”(毫秒-自从-时代)。 Converting
undefined
to a number results in NaN
("not a number"); 将
undefined
转换为数字会产生NaN
(“非数字”); dates using NaN
as their "time value" are called invalid dates. 使用
NaN
作为其“时间值”的日期称为无效日期。 Using getMonth
and similar on them returns NaN
. 在它们上使用
getMonth
和类似的返回NaN
。
If you want the current date, just don't use the argument at all: 如果要使用当前日期,则根本不用参数:
var date = new Date();
Here's a working version of your code (with minimal modifications) using an id
and not passing undefined
into Date
: 这是使用
id
且未将undefined
传递给Date
代码的有效版本(经过最少的修改):
function getInfo() { var date = new Date(); var month = date.getMonth(); var test = "print"; document.getElementById('lol').value = month; }
<input type=text name=lol id=lol> <input type=button onClick=getInfo() value=lol>
And here's one using querySelector
with the name
: 这是一个使用
querySelector
的name
:
function getInfo() { var date = new Date(); var month = date.getMonth(); var test = "print"; document.querySelector('[name=lol]').value = month; }
<input type=text name=lol> <input type=button onClick=getInfo() value=lol>
Side note: I really would put the content of your onClick
attribute in quotes, because it contains non-alphanumeric characters. 旁注:我真的会将
onClick
属性的内容括在引号中,因为它包含非字母数字字符。 According to the specification , your markup should work, and does on Chrome (the unquoted syntax allows anything but spaces, "
, '
, <
, >
, and `
), but... 根据规范 ,您的标记应该可以正常运行,并且可以在Chrome上运行(无引号的语法允许使用空格,
"
, '
, <
, >
和`
),但是...
I suggest use Jquery 我建议使用Jquery
<input type=text id="somethingElse">
<input type=button id="something">
$( "#something" ).click(function() {
var currentMonth = (new Date).getMonth() + 1;
$( "#somethingElse" ).val(currentMonth);
});
You're missing quotes and also the id
: 您缺少引号和
id
:
<input type="text" id="lol" name="lol">
<input type="button" onClick="getInfo();" value="lol">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.