簡體   English   中英

Javascript...為什么這個go不會進入if語句的else部分?

[英]Javascript... Why will this not go into the else section of the if statement?

在 codepen 上查看此處... [ https://codepen.io/johnstonf/pen/qBbbpaV?editors=1011]

Javascript ... 當條件變量顯示它應該時,為什么這不是 go 進入 if 語句的 else 部分。 它第一次工作(單擊按鈕),但第二次,它應該反轉 function,以清除剪貼板,但它再次執行 if 部分。

<div id="fj33">
    <h5>Sensitive Data For Clipboard</h5>
    <p>Sensitive Data Line 2</p>
</div>
<div id="fj34">
    <h5>Clipboard Cleared</h5>
    <p>Sensitive Data Gone</p>
</div>

<button id="fjCopyTxtButton" class="btn btn-primary" onclick="copyText()">Copy Text to Clipboard!!</button>

<script type="text/javascript">
  function copyText() {
    fjChk=fjCopyTxtButton.innerHTML.includes("COPIED");
    console.log("CURRENT-(Before-IF):",fjChk);
    if(fjChk != "true")
    {
      console.log("CURRENT1:",fjChk);
      //console.log("...now false");
      var range, selection, worked, fj, fj2;
      fj=document.querySelector('#fj33');
      fj2=fj.innerText
      console.log(fj2);
      var copyhelper = document.createElement("textarea");
      copyhelper.className = 'copyhelper'
      document.body.appendChild(copyhelper);
      copyhelper.value = fj2;
      fj3=copyhelper.value;
      copyhelper.select();
      document.execCommand("copy");
      document.body.removeChild(copyhelper);
      document.querySelector('#fjCopyTxtButton').style.backgroundColor='yellow';
      document.querySelector('#fjCopyTxtButton').style.color='red';
      document.querySelector('#fjCopyTxtButton').innerHTML = '<h3>Text COPIED</h3>';
    } else {
      console.log("CURRENT2:",fjChk);
      console.log("...now true in else section");   
      fj=document.querySelector('#fj34');
      fj2=fj.innerText
      console.log(fj2);
      var copyhelper = document.createElement("textarea");
      copyhelper.className = 'copyhelper'
      document.body.appendChild(copyhelper);
      copyhelper.value = fj2;
      fj3=copyhelper.value;
      copyhelper.select();
      document.execCommand("copy");
      document.body.removeChild(copyhelper);
      document.querySelector('#fjCopyTxtButton').style.backgroundColor='green';
      document.querySelector('#fjCopyTxtButton').style.color='black';
      document.querySelector('#fjCopyTxtButton').innerHTML = '<h3>Text CLEARED</h3>';
  }};


 </script>
if (fjChk != "true")

您正在檢查字符串"true" ,但 fjChk 不是字符串。 您需要檢查 boolean。

if (fjChk !== true)

要么

if (!fjChk)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM