简体   繁体   English

Javascript 没有取消单击带有链接的按钮

[英]Javascript not un-clicking button with link

Newish to javascript, but I do't know why the below is not working.对 javascript 很陌生,但我不知道为什么下面的内容不起作用。 I am trying to use javascript to make a link become active after a button has become clicked.我正在尝试使用 javascript 在单击按钮后使链接变为活动状态。

The user clicks a button that updates the HTML, changes the class and adds a html link.用户单击更新 HTML、更改 class 并添加 html 链接的按钮。 The code works the first time -- the HTML is updated correctly.代码第一次运行——HTML 已正确更新。 However if the user decides to un-click the button nothing happens.但是,如果用户决定取消单击该按钮,则不会发生任何事情。

Javascript: Javascript:

function agree() {
  let agreement = document.getElementById("agreement");
  let agree = document.getElementById("agree");

  agree.onclick = () => {
    if (agree.value === true) {
      agreement.innerHTML = `
             <button id="agree" class="agree--no" value="false"></button>I agree
                  <div class="btn--no-schedule">
                    <a href="#" > No SCHEDULE  </a>
                </div> `
    } else {
        agreement.innerHTML = `
            <button id="agree" class="agree--checked" value="true"><i class="fas fa-lg fa-check-square"></i></button>I agree
          <div class="btn--agree-schedule">
                <a href="http://google.com" > yes SCHEDULE  </a>
       </div> `
    }
  }
};

HTML HTML

<div id="agreement">
  <button id="agree" class="agree--no" value="false"></button>I agree
    <div class="btn--no-schedule">
      <a href="#" > SCHEDULE  </a>
    </div>
</div>

I also tried我也试过

<button onclick=“agree();” id="agree" class="agree--no" value="false"></button>

but get a type error agree() is not a function.但得到一个类型错误同意()不是 function。

Any help would be appreciated任何帮助,将不胜感激

Try this code:试试这个代码:

 function ChangeContent(checkbox) { var el = document.getElementsByClassName("schedule-agreement")[0]; (checkbox.checked)? el.innerText = "No SCHEDULE": el.innerText = "Yes SCHEDULE" }
 <div id="agreement"> <input type="checkbox" onchange="ChangeContent(this)"> I agree <div> <a href="#" class="schedule-agreement">SCHEDULE</a> </div> </div>

Explanation:解释:

First of all, add a change event listener to the input checkbox.首先,在输入复选框中添加一个更改事件侦听器。 Then, whenever it is run, check if it is checked.然后,每当它运行时,检查它是否被选中。 If it is, change the link's innerText to "No SCHEDULE", otherwise, change it to "Yes SCHEDULE".如果是,则将链接的innerText更改为“No SCHEDULE”,否则,将其更改为“Yes SCHEDULE”。

If you need to use a button, then I would recommend adding a click event listener (or an onclick inline event listener), and changing the link innerText ONLY - not the whole HTML.如果您需要使用按钮,那么我建议添加一个click事件侦听器(或onclick内联事件侦听器),并仅更改链接innerText - 而不是整个 HTML。

In that case, here's a separate demo:在这种情况下,这是一个单独的演示:

 var isChecked = false function ChangeContent() { isChecked =.(isChecked) if (isChecked) { document.getElementsByClassName("agree")[0].innerText = "✔️ I agree" document.getElementsByClassName("schedule-agreement")[0].innerText = "No SCHEDULE" } else { document.getElementsByClassName("agree")[0].innerText = "❌ I agree" document.getElementsByClassName("schedule-agreement")[0].innerText = "Yes SCHEDULE" } }
 <div id="agreement"> <button class="agree" onclick="ChangeContent()">I agree</button> <div class="btn--no-schedule"> <a href="#" class="schedule-agreement">SCHEDULE</a> </div> </div>

There are two errors here.这里有两个错误。

First, the click event is lost when you reset the agreement's innerHTML, second, agree.value is a string, and thus will never be "=== true".首先,重置协议的innerHTML时点击事件丢失,其次, agree.value是一个字符串,因此永远不会是“=== true”。

There are multiple ways of fixing it.有多种修复方法。 One way is changing the innerHTML part so the event isn't lost.一种方法是更改 innerHTML 部分,这样事件就不会丢失。 Also, changing the condition to === 'true'此外,将条件更改为=== 'true'

Like so:像这样:

HTML: HTML:

<div id="agreement">
  <button id="agree" class="agree--no" value="false"></button>I agree
    <div id="schedule-btn" class="btn--no-schedule">
      <a href="#" > SCHEDULE  </a>
    </div>
</div>

JS: JS:

function agree() {
  const agreeBtn = document.getElementById("agree");
  const scheduleBtn = document.getElementById("schedule-btn");

  agreeBtn.onclick = () => {
    if (agreeBtn.value === "true") {
      agreeBtn.value = false;
      scheduleBtn.innerHTML = `
          <div class="btn--no-schedule">
            <a href="#" > No SCHEDULE  </a>
          </div>
        `;
    } else {
      agreeBtn.value = true;
      scheduleBtn.innerHTML = `
          <div class="btn--agree-schedule">
            <a href="http://google.com" > yes SCHEDULE  </a>
          </div>
        `;
    }
  };
}

Edit: I tried to alter your code as little as possible编辑:我试图尽可能少地改变你的代码

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

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