亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

這里有什么問題?

這里有什么問題?

一只甜甜圈 2021-11-04 15:33:50
我構建了一個沒有任何 html 的顯示和隱藏密碼應用程序,只是 html、head、body 元素和腳本元素作為練習,但這里有錯誤:const inputOne = document.createElement('input');const attrOne = document.createAttribute('type');attrOne.value = 'password';inputOne.setAttributeNode(attrOne);const btnOne = document.createElement('button');btnOne.innerHTML = 'Show Password';document.body.appendChild(inputOne);const BRBetween = document.createElement('br');const BRsBetween = document.createElement('br');document.body.appendChild(BRBetween);document.body.appendChild(BRsBetween);document.body.appendChild(btnOne);const shHiPassword = function shHiPass() {  if (inputOne.type == 'password') {    inputOne.type = 'text';    inputTwo.innerHtml = 'Hide Password';  } else {    inputOne.type = 'password'    inputTwo.innerHtml = 'Show Password';  }};const attrTwo = document.createAttribute('onclick');attrTwo.value = shHiPassword;btnOne.setAttributeNode(attrTwo);它只是給我密碼字段和按鈕,當我單擊按鈕時沒有任何反應。我認為功能有問題,但我不知道在哪里......
查看完整描述

2 回答

?
蝴蝶刀刀

TA貢獻1801條經驗 獲得超8個贊

我試圖修復你的代碼,現在你可以嘗試一下,它會按你的意愿工作。我已經使用單擊事件偵聽器而不是將其設置為元素。并用setAttribute替換了setAttributeNodes


const inputOne = document.createElement('input'),

      attrOne = document.createAttribute('type');


inputOne.setAttribute('type', 'password');

const btnOne = document.createElement('button');



btnOne.innerHTML = 'Show Password';

document.body.appendChild(inputOne);


const BRBetween = document.createElement('br');


const BRsBetween = document.createElement('br');


document.body.appendChild(BRBetween);

document.body.appendChild(BRsBetween);

document.body.appendChild(btnOne);


btnOne.addEventListener('click', function(e){

  e.preventDefault();

if (inputOne.type == 'password') {

        inputOne.type = 'text';

        this.innerHTML = 'Hide Password';

 } else {

        inputOne.type = 'password';


    this.innerHTML = 'Show Password';

 }

});


查看完整回答
反對 回復 2021-11-04
?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

嘗試將您的onclick屬性設置為字符串值。這是一個工作示例:


const inputOne = document.createElement('input');

const attrOne = document.createAttribute('type');

attrOne.value = 'password';

inputOne.setAttributeNode(attrOne);


const btnOne = document.createElement('button');

btnOne.innerHTML = 'Show Password';

document.body.appendChild(inputOne);


const BRBetween = document.createElement('br');

const BRsBetween = document.createElement('br');

document.body.appendChild(BRBetween);

document.body.appendChild(BRsBetween);

document.body.appendChild(btnOne);


function shHiPassword() {

  if (inputOne.type == 'password') {

    inputOne.type = 'text';

    btnOne.innerHTML = 'Hide Password';

  } else {

    inputOne.type = 'password'

    btnOne.innerHTML = 'Show Password';

  }

};


const attrTwo = document.createAttribute('onclick');

attrTwo.value = 'shHiPassword()';

btnOne.setAttributeNode(attrTwo);


查看完整回答
反對 回復 2021-11-04
  • 2 回答
  • 0 關注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號