简体   繁体   English

无法获取JavaScript代码在Firefox上运行

[英]Can't get JavaScript code to work on Firefox

I've written a code, on which when the form field reaches its max length (in this example 1) the cursor moves to next field. 我编写了一个代码,当表单字段达到其最大长度(在本示例中为1)时,光标将移至下一个字段。

JS: JS:

window.onload=function(){
var container = document.getElementsByClassName("container")[0];
container.onkeyup = function(e) {
    var target = e.srcElement;
    var maxLength = parseInt(target.attributes["maxlength"].value, 10);
    var myLength = target.value.length;
    if (myLength >= maxLength) {
        var next = target;
        while (next = next.nextElementSibling) {
            if (next == null)
                break;
            if (next.tagName.toLowerCase() == "input") {
                next.focus();
                break;
            }
        }
    }
}
}

HTML: HTML:

<div class="container">
<label><input type="text" name="pin1" maxlength="1" pattern="\d{1}" onkeypress="return isNumber(event)" required/>&nbsp;-&nbsp;
<input type="text" name="pin2" maxlength="1" pattern="\d{1}" onkeypress="return isNumber(event)" required/>&nbsp;-&nbsp;
<input type="text" name="pin3" maxlength="1" pattern="\d{1}" onkeypress="return isNumber(event)" required/>&nbsp;-&nbsp;
<input type="text"  name="pin4" maxlength="1" pattern="\d{1}" onkeypress="return isNumber(event)" required/>
</label>
</div>

The form works just fine on Google Chrome and other mobile browsers, but it isn't working on Firefox. 该表格在Google Chrome和其他移动浏览器上都可以正常使用,但在Firefox上却无法使用。

Please help me to get it work on cross browser. 请帮助我使其在跨浏览器上运行。

The problem is you are using the non-standard event property srcElement . 问题是您正在使用非标准事件属性srcElement

var target = e.srcElement;

From MDN on Event.srcElement : MDN上的Event.srcElement

Event.srcElement is a proprietary alias for the standard Event.target property. Event.srcElement是标准Event.target属性的专有别名。 It is specific to old versions of Microsoft Internet Explorer. 它特定于旧版本的Microsoft Internet Explorer。

The only reason it is working in Chrome is this browser currently also aliases this property for compatibility reasons. 它在Chrome中运行的唯一原因是该浏览器当前也出于兼容性原因对该属性进行了别名。

Use this instead: 使用此代替:

var target = e.target;

Every version if IE requiring srcElement is obsolete, so there is no reason to use this property today. 如果IE需要srcElement则每个版本srcElement过时,因此,今天没有理由使用此属性。

(I'm assuming you have isNumber defined somewhere, else this will also be a problem.) (我假设您在isNumber定义了isNumber ,否则这也将是一个问题。)

In both Chrome and Firefox, the function isNumber() is not defined. 在Chrome和Firefox中,均未定义函数isNumber()。

The other issues is that you're using a non-standard event property srcElement, you should use e.target instead. 另一个问题是您使用的是非标准事件属性srcElement,而应改用e.target。 https://developer.mozilla.org/en/docs/Web/API/Event/srcElement https://developer.mozilla.org/en/docs/Web/API/Event/srcElement

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

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