如何在Javascript中将输入类型更改为纯文本

如何在Javascript中将输入类型更改为纯文本,javascript,html,Javascript,Html,我做了一个按钮,但当我按下按钮时,我想将按钮更改为文本输出。我不想更改按钮内的文本。我想将按钮本身更改为文本 这是我的钮扣 <script type="text/javascript"> function check1() { const answer = document.getElementById("question1").innerText; const mybutton = document.getElementByI

我做了一个按钮,但当我按下按钮时,我想将按钮更改为文本输出。我不想更改按钮内的文本。我想将按钮本身更改为文本

这是我的钮扣

<script type="text/javascript">
        function check1() {
          const answer = document.getElementById("question1").innerText;
          const mybutton = document.getElementById("button1");
          if(answer=="Seoul") mybutton.innerText = "CORRECT"
          else mybutton.innerText = "Seoul"
        }
      </script>

这是我未完成的版本,如果答案正确,则将按钮更改为文本“正确”,如果答案错误,则将按钮更改为文本“首尔”。

您可以使用按钮文本创建一个具有相同文本的元素,并首先隐藏此元素。当您单击按钮时,显示元素并隐藏按钮。

您可以使一个元素具有与按钮文本相同的文本,并首先隐藏此元素。当您单击button show元素并隐藏按钮时。

HTML元素没有innerText属性,请改用。您还应该删除该按钮,并使用创建一个值为的文本节点。最后,在输入元素之后插入创建的文本元素:

功能检查1{ const answer=document.getElementByIdquestion1; const mybutton=document.getElementByIdbutton1; mybutton.remove; var-textNode; ifanswer.value.trim==首尔{ textNode=document.createTextNodeCORRECT; } else textNode=document.createTextNodeSeoul; //在应答元素后插入文本节点 answer.parentNode.insertBeforetextNode,answer.nextSibling; } HTML元素没有innerText属性,请改用。您还应该删除该按钮,并使用创建一个值为的文本节点。最后,在输入元素之后插入创建的文本元素:

功能检查1{ const answer=document.getElementByIdquestion1; const mybutton=document.getElementByIdbutton1; mybutton.remove; var-textNode; ifanswer.value.trim==首尔{ textNode=document.createTextNodeCORRECT; } else textNode=document.createTextNodeSeoul; //在应答元素后插入文本节点 answer.parentNode.insertBeforetextNode,answer.nextSibling; }
通过按id获取元素并将类型转换为文本,可以将按钮字段更改为文本字段

document.getElementById("button1").setAttribute("type","text");
要禁用转换后的文本字段,可以这样做

document.getElementById("button1"). disabled = true;
完整代码:

<script type="text/javascript">
function check1() {
  //Fetch elements and answers
  const answer = document.getElementById("question1").innerText;
  const mybutton = document.getElementById("button1");

  //Change button to text field with changing its type attriube to text
  mybutton.setAttribute("type","text");

  //Disable the newly created text field
  mybutton.disabled = true;

  if(answer === "Seoul"){
    mybutton.value = "CORRECT";
  }else{
    mybutton.value = "Seoul";
  }
}
</script>

最后,在进行比较时,使用三重等于===而不是双等于==。Double equals==在执行比较之前将变量值转换为相同的类型。triple equals===不执行任何类型转换强制,仅当值和类型都相同时才返回true

您可以通过按id获取元素并将类型转换为文本来将按钮字段更改为文本字段

document.getElementById("button1").setAttribute("type","text");
要禁用转换后的文本字段,可以这样做

document.getElementById("button1"). disabled = true;
完整代码:

<script type="text/javascript">
function check1() {
  //Fetch elements and answers
  const answer = document.getElementById("question1").innerText;
  const mybutton = document.getElementById("button1");

  //Change button to text field with changing its type attriube to text
  mybutton.setAttribute("type","text");

  //Disable the newly created text field
  mybutton.disabled = true;

  if(answer === "Seoul"){
    mybutton.value = "CORRECT";
  }else{
    mybutton.value = "Seoul";
  }
}
</script>

最后,在进行比较时,使用三重等于===而不是双等于==。Double equals==在执行比较之前将变量值转换为相同的类型。虽然triple equals===不执行任何类型转换强制,并且仅当值和类型都相同且文本没有innerText时才返回true,但请改用value:

功能检查1{ const answer=document.getElementByIdquestion1.innerText; const mybutton=document.getElementByIdbutton1; ifanswer==首尔{ mybutton.parentNode.removeChildmybutton; var t=document.createTextNodeCorrect; document.body.appendChildt;} 否则{ mybutton.parentNode.removeChildmybutton; var t=document.createTextNodeSeoul; document.body.appendchild; } }
输入类型按钮和文本没有innerText,请改用值:

功能检查1{ const answer=document.getElementByIdquestion1.innerText; const mybutton=document.getElementByIdbutton1; ifanswer==首尔{ mybutton.parentNode.removeChildmybutton; var t=document.createTextNodeCorrect; document.body.appendChildt;} 否则{ mybutton.parentNode.removeChildmybutton; var t=document.createTextNodeSeoul; document.body.appendchild; } }
如果您只是用值替换innerText,它将工作如果您只是用值替换innerText,它将工作我尝试了此操作,但这只是将按钮更改为具有值的输入文本框。如果我想将按钮更改为不可编辑的文本,该怎么办?我尝试了此操作,但这只是将按钮更改为具有值的输入文本框。如果我想将按钮更改为不可编辑的文本,该怎么办?问题不是更改按钮的值,而是将按钮标记更改为文本或div,并将文本作为一个文本answer@AyushKatiyar,你说得对,更新了答案,+1您的答案:问题不是关于更改按钮的值,而是将按钮标记更改为text或div,并将文本作为answer@AyushKatiyar,你是对的,更新了答案,+1你的答案: