简体   繁体   English

如何使用 javascript 让我的按钮工作?

[英]How to get my button to work using javascript?

I am creating an Google chrome extension and I am trying to get my stop/stop logging button to work within my function named Logger.我正在创建一个 Google chrome 扩展,我正在尝试让我的停止/停止记录按钮在我名为 Logger 的 function 中工作。 When the button is pressed it doesn't react to the function I wrote, Currently it is displaying the stop-button but I want it to display the start-button when clicked.当按下按钮时,它不会对我写的 function 做出反应,目前它正在显示停止按钮,但我希望它在单击时显示开始按钮。 I hope I explained that to some understanding but do anyone possibly know why my function is not working?我希望我解释了一些理解,但有人可能知道为什么我的 function 不工作吗?

Below is my current javascript function and html:下面是我现在的javascript function和html:

popup.js弹窗.js

//attempt to get start/stop logging buttons to work
function Logger(isLogging, notLogging) {
    if (isLogging = true, notLogging = false) {
        addRow();
        document.getElementById("click-start").style.display = "block";
        document.getElementById("click-stop").style.display = "none";    

    }
    if (isLogging = false, notLogging = true) {
        document.getElementById("click-start").style.display= "none";
        document.getElementById("click-stop").style.display= "block";
    }
}

//button to start logging
document.addEventListener('DOMContentLoaded', function() {
  document.getElementById("click-start").addEventListener("click", Logger(true, false));


});

//button to stop logging
document.addEventListener('DOMContentLoaded', function() {
  document.getElementById("click-stop").addEventListener("click", Logger(false, true));


});

popup.html弹出窗口.html

<!--Start button of logging-->
    <button class="button button1" id="click-start" >
    <u> Start Logging </u>
    </button>
    
    <!--Stop button of logging-->
    <button class="button button2" id="click-stop" >
    <u> Stop Logging </u>
    </button>

Image of current output --button currently doesnt react当前 output 的图像 --按钮当前没有反应

This may help to get the core functionality working, this implementation can be much improved这可能有助于使核心功能正常工作,此实现可以大大改进

const btnStart = document.getElementById("click-start");
const btnStop = document.getElementById("click-stop");

//attempt to get start/stop logging buttons to work
function Logger(isLogging) {
    console.log(isLogging)
    if (isLogging) {
        btnStart.style.display = "block";
        btnStop.style.display = "none";
    }else{
        btnStart.style.display = "none";
        btnStop.style.display = "block";
    }
}

//button to start logging
document.addEventListener("DOMContentLoaded", function () {
    btnStart.addEventListener("click", function() {Logger(false)}); 
    btnStop.addEventListener("click", function() {Logger(true)});
});

You have to try to keep the queries to the DOM to a minimum.您必须尽量减少对 DOM 的查询。 Have a look at the toggle method it will help to make your code a bit leaner and easier to maintain看一下切换方法,它将有助于使您的代码更精简且更易于维护

I'm not sure if the Logger function gets executed if you use it like this in the addEventListener.如果您在 addEventListener 中像这样使用它,我不确定是否会执行 Logger function。

Maybe you can give it a try like this:也许你可以像这样尝试一下:

document.addEventListener('DOMContentLoaded', function() {
  document.getElementById("click-start").addEventListener("click", function () {
    Logger(true, false))
  };
});

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

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