繁体   English   中英

为什么“showPass”function 返回 null?

[英]Why "showPass" function is returning null?

我正在尝试编写 function 但有一些错误。 我的“showPass”function 正在返回 null

<input id="userid" type="password" value="12345">
<button id="showPassButton" onclick="showPass(userid, showPassButton)">Show</button>
    
<script>
       function showPass(passId, btnId) {  
            const pass = document.getElementById('passId');
            console.log(pass);
            const btn = document.getElementById('btnId');
            console.log(btn);
        }
</script>

您必须使用onclick="showPass('userid', 'showPassButton')">而不是onclick="showPass(userid, showPassButton)">因为getElementById需要一个字符串。 请参考以下片段。

 function showPass(passId, btnId) { const pass = document.getElementById(passId); console.log(pass); const btn = document.getElementById(btnId); console.log(btn); }
 <input id="userid" type="password" value="12345"> <button id="showPassButton" onclick="showPass('userid', 'showPassButton')">Show</button>

暂无
暂无

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

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