簡體   English   中英

document.getElementById 和 document.querySelector 不會在同一個元素上工作兩次

[英]document.getElementById and document.querySelector won't work twice on the same element

我正在創建一個基於文本的冒險游戲,我正在使用按鈕來更改文本。 我有一個開始按鈕,當你按下它時,它會改變文本。 但是我有一個是按鈕,你 NPC 告訴你按下它,但沒有任何反應。 我嘗試使用 ctrl-shift-I,它說沒有錯誤或警告。 我嘗試了document.querySelector而不是document.getElementById並且得到了相同的結果。 我還嘗試將其放入數據 function 中,但沒有任何進展。

這是我在正文代碼中的 HTML

<div class="container">
  <div>
    <p id="text">The D-LABS Space Adventure <br> D-LABS just got more exciting! In this new version, you are lost in space. You need to make the right choices to escape alive and keep you research. Do you have what it takes to win? Press "START" to play.</p>
  </div>
  <div id="option-buttons" class="btn-grid">
  <button class="btn" id="yes" onClick-"yessy()">YES</button>
  <button class="btn" id="start" onClick="data()">START</button>
  </div>
</div>

這是我的 javascript 代碼(當我使用 querySelector 時,我使用了“#text”而不是“text”。)

function data() {
    document.getElementById("text").innerHTML = name+", this is Professer Andromeda. Reply yes if you here this signal. I repeat, "+name+", reply yes if you hear this signal";
}
function yessy() {
    document.getElementById("text").innerHTML = "Okay, good. You are able to hear me "+name+". I am locating you right now...found you! You are in the asteroid belt, right next to the dwarf planet Ceres. Did you know, Ceres was the first object spotted in the asteroid belt? Sorry "+name+". I was just off task. Okay, you can choose to go east, north, or west. Right now, my radar says there is some activity east, but I do not know what it is. I will talk more after you move. Professor Andromeda out.";
}

我是編程新手,並且一直在尋找解決此問題的方法。 誰能指出我正確的方向? 謝謝。

您在“是”按鈕上出現語法錯誤,而不是:

onClick-"yessy()"

改變是

onClick="yessy()"

暫無
暫無

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

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