Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么当我说;var";并使其等于document.getElementById,它不';不管用?_Javascript_Variables - Fatal编程技术网

Javascript 为什么当我说;var";并使其等于document.getElementById,它不';不管用?

Javascript 为什么当我说;var";并使其等于document.getElementById,它不';不管用?,javascript,variables,Javascript,Variables,好的,我要问的是倾斜的。我有这样的密码: function fromPassToText(inputId) { var theThing = document.getElementById(inputId).type; if (theThing == "password") theThing = "text"; else theThing = "password"; } 它不会进入if

好的,我要问的是倾斜的。我有这样的密码:

function fromPassToText(inputId) {
        var theThing = document.getElementById(inputId).type;
        if (theThing  == "password")
            theThing  = "text";
        else
            theThing  = "password";
    }
它不会进入if。当我用
document.getElementById(inputId)重命名所有
theThing
时,键入它的正确名称

我是新来的,如果你能帮助我,我会很感激的

谢谢

当你这样做的时候

theThing  = "text";

您只是在更改局部变量。我假设您需要更改DOM元素的属性,唯一的方法是使用
document.getElementById(inputId.type=


顺便说一下,这也行

function fromPassToText(inputId) {
    var theThing = document.getElementById(inputId).type;
    if (theThing  == "password")
        document.getElementById(inputId).type = "text";
    else
        document.getElementById(inputId).type = "password";
}

我非常确定,您的代码是“进入if语句”

我猜,你在想,为什么类型没有改变。它没有,因为您只更改了变量

您要做的是:

function fromPassToText(inputId) {
    var theThing = document.getElementById(inputId);
    if (theThing.type  == "password")
        theThing.type  = "text";
    else
        theThing.type  = "password";
}

通过这种方式,您可以更改元素,而不仅仅是变量。

到目前为止,答案仅部分正确

他们认为这不起作用是正确的:

var theThing = document.getElementById(inputId).type;
theThing  = "text";
这是因为您将获得一个基元类型值(一个字符串),Javascript将按值传递这些值

然而,这将起作用:


这是因为您正在将输入设置为对象,对象通过引用传递。因此,出于所有意图和目的,
输入
就是元素本身。

这里需要包含相对标记和函数调用。很难从你如何写问题来判断什么是不起作用的。最好做
var theInput=document.getElementById(inputId);如果(theInput.type==“password”)theInput.type=“text”;否则输入。type=“密码”-尽管需要注意的是,一些浏览器不支持在输入出现在页面上后更改输入类型,而其他浏览器则作为标准提供此功能。奇怪的是,“Internet Explorer”是我在这两种情况下想到的浏览器……哦,是的。我明白了。感谢波塔托皮林斯和尼特黑暗的绝对,你真的帮助我理解了这一点!:)祝你愉快day@Niet黑暗的绝对-是的,这肯定更清楚,但这会让人们在更改这段代码之前三思而后行:-)@BG DiscoX-你最好隐藏/显示文本/密码控件,而不是更改控件类型。谢谢你,黑暗的绝对@potatopeelings——我这样做是因为你按下一个按钮,你可以看到你的密码,如果你想检查它是否是它需要的。
var theThing = document.getElementById(inputId).type;
theThing  = "text";
var theInput = document.getElementById(inputId);
theInput.type  = "text";