簡體   English   中英

在單擊另一個按鈕之前檢查用戶單擊了哪個按鈕

[英]Checking which button was clicked by the user before clicking on another button

例如,我試圖弄清楚如何寫這個。 當用戶單擊id "niekket"的按鈕時,它現在需要用戶專門單擊該按鈕。 在用戶單擊"niekket"並希望通過單擊id "resetbtn"的按鈕來重置值后。 id("uitvoer").innerHTML = // output (a string) depends on which button has been clicked (on niekket or pap) by the user before clicking on the resetbtn它應該輸出這個alert("You clicked on button niekket") else alert("You clicked on the button pap before clicking on the resetbutton")

任何幫助,將不勝感激!

 function personen(voornaam, achternaam, geboortejaar) { this.voornaam = voornaam; this.achternaam = achternaam; this.geboortejaar = geboortejaar; this.gegevens = function () { return this.voornaam + " " + this.achternaam + " " + this.geboortejaar; }; this.wijzigVoornaam = function (vNaam) { this.voornaam = vNaam; }; this.wijzigAchternaam = function (aNaam) { this.achternaam = aNaam; }; this.wijzigGeboortejaar = function (jaar) { this.geboortejaar = jaar; }; } var niekket = new personen("Niek", "Henk", 1800); var pap = new personen("Joop", "Freek", 1950); function id(id) { var allId = document.getElementById(id); return allId; } function allEvents() { var idClicked; id("niekket").onclick = function () { toonGegevens(niekket); idClicked = "Niekket"; }; id("pap").onclick = function () { toonGegevens(pap); idClicked = "pap"; }; id("resetbtn").onclick = function () { id("uitvoer").innerHTML = idClicked; }; } allEvents(); function toonGegevens(p) { id("uitvoer").innerHTML = p.gegevens(); }
 <p id="uitvoer">Klik op de knop en zie deze invoer wijzigen</p> <button class="btn" id="niekket">Niekket</button> <button class="btn" id="pap">Pap</button> <button id="resetbtn">resetbutton</button>

您可以將上次點擊的 ID 存儲在某個變量中,例如:

var lastClickedId;

function allEvents() {
    id("niekket").onclick = function () {
        toonGegevens(niekket);
        lastClickedId = "niekket";
    };
}

然后在需要時使用這個lastClickedId變量。

暫無
暫無

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

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