简体   繁体   English

Javascript不适用于Chrome和Firefox,但适用于IE

[英]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 : 这是一个使用querySelectorname

 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.

相关问题 该Javascript在IE中不起作用,但在Chrome,Firefox和Opera中起作用 - This Javascript doesn't work in IE but works in Chrome, Firefox and Opera JavaScript时钟可在Chrome上使用,但不适用于Firefox或IE - JavaScript clock works on Chrome but doesn't work with Firefox or IE JavaScript代码在chrome上有效,在IE和Firefox上无效 - JavaScript code works on chrome, doesn't work on IE and firefox Javascript 自动完成功能适用于 IE 和 Chrome,不适用于 Firefox - Javascript autocomplete works in IE and Chrome, doesn't work in Firefox Javascript仅在IE Quirks,7和Chrome和Firefox中有效。 在IE 8或9标准中不起作用 - Javascript only works in IE Quirks, 7 and Chrome and Firefox. Doesn't work in IE 8 or 9 Standards EmberData unloadAll在IE8上不起作用。 适用于Chrome / FireFox - EmberData unloadAll doesn't work on IE8. Works on Chrome/FireFox .unbind(&#39;submit&#39;)。submit()在Chrome / IE中有效,但在Firefox中无效 - .unbind('submit').submit() works in Chrome/IE but doesn't work in Firefox JS正则表达式代码不适用于Firefox,但适用于Chrome和IE - JS regex code doesn't work with firefox, but works on chrome and IE jQuery模糊在Firefox和Chrome中不起作用,但在IE9中起作用 - Jquery blur doesn't work in Firefox and Chrome but works in IE9 Jquery / Javascript和Css条件在firefox和IE上不起作用。 它适用于Chrome - Jquery/Javascript and Css condition doesn't work on firefox and IE. It works on Chrome
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM