0

好的,我正在编写一些代码,有人可以将其用作书签来实时混淆输入框的值...

var elements = document.getElementsByTagName("input");
for (var ii = 0; ii < elements.length; ii++) {

    elements[ii].addEventListener("keyup", function() {

        var input = elements[ii].value;
        var a = [""];
        var gen;
        for (x in input) {
            a[x] = input[input.length - x - 1];
        };
        for (x in a) {
            var gen = gen + a[x];
        };
        output = gen.replace('undefined', '');
        elements[ii].value = output;
    }, false);
};

此代码(前面带有 JavaScript:)对任何输入框都没有影响。谁能明白为什么?

谢谢!

4

2 回答 2

1

Use this instead of elements[ii] inside the addEventListener code. Cos elements[ii] will have no meaning inside that code.

于 2012-12-15T17:20:31.500 回答
1

我在你的代码中做了一些修复。这是修改后的版本:

var elements = document.getElementsByTagName("input");
for (var ii = 0; ii < elements.length; ii++) {

    elements[ii].addEventListener("keyup", function() {

        var input = this.value;
        var a = [];
        var gen;
        for (var x in input) {
            a[x] = input[input.length - x - 1];
        };
        for (x in a) {
            var gen = gen + a[x];
        };
        output = gen.replace('undefined', '');
        this.value = output;
    }, false);
};

请查看演示

于 2012-12-15T17:29:02.080 回答