亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何用點擊計數值替換圖像而不是添加帶有點擊計數值的圖像?(刪除圖像+添加新圖像)

如何用點擊計數值替換圖像而不是添加帶有點擊計數值的圖像?(刪除圖像+添加新圖像)

qq_笑_17 2023-07-14 15:15:39
所以我有這個應用程序,我點擊按鈕,嵌套的 div 出現,最里面的 div 中帶有圖像。textarea 會跟蹤按鈕被單擊的次數,并且每次單擊時,嵌套的 div 都會保留,但圖像會在初始圖像和第二個圖像之間交換?,F在,在第一次單擊時,一切都是正確的:第一次單擊時出現嵌套的 div,第一個圖像位于 div 中,并且計數器更新。此后的每次點擊:嵌套的 div 保留并且不重復(這是正確的),計數器更新(這是正確的),圖像在偶數和奇數單擊之間交替,但它們都被添加到彼此旁邊。我需要的是讓它們相互交換/替換。我不知道我是否需要重做我的 addbutterfly 函數,這樣它們就不會只是不斷添加,而只會根據文本區域的點擊值添加 1 個圖像我的另一個想法是添加一個函數來刪除偶數或奇數的圖像,同時添加相反的偶數或奇數的圖像。任何幫助表示贊賞var i = 0;function runTogether1()   {    const value = parseInt(document.getElementById('id1').value, 10);    if (isNaN(value)) {      addDiv();      addDiv2();      addDiv3();      addbutterfly();      incrementValue();  }    else{      addbutterfly();      incrementValue();    }  }function addDiv()   {    var div1 = document.createElement('div');    div1.classList.add('div1');    document.body.appendChild(div1);  }function addDiv2()   {    var div2 = document.createElement('div');    div2.classList.add('div2');    document.getElementsByClassName("div1")[i].appendChild(div2);  }function addDiv3()   {    var div3 = document.createElement('div');    div3.classList.add('div3');    document.getElementsByClassName("div2")[i].appendChild(div3);  }function addbutterfly() {  var img = document.createElement('img');  // Get the value of the "textfield"  const value = parseInt(document.getElementById('id1').value, 10);  // If the value is even, add "bfly2.gif", otherwhise add "bfly1.gif"  img.src = value % 2 === 0 ? "bfly2.gif" : "bfly1.gif";  document.getElementsByClassName("div3")[0].appendChild(img);}function incrementValue(){    var value = parseInt(document.getElementById('id1').value, 10);    value = isNaN(value) ? 0 : value;    value++;    document.getElementById('id1').value = value;}    .div1 {  background-color: red;  margin: 1em;  height: 500px;  width: 500px;  justify-content: center;  align-items: center;}.div2 {  background-color: yellow;  height: 300px;  width: 300px;}
查看完整描述

1 回答

?
桃花長相依

TA貢獻1860條經驗 獲得超8個贊

您不需要繼續添加圖像,而只需更改源


我還更改為 addEventListener 和 querySelector 因為它更優雅


document.getElementById("add").addEventListener("click",function() {

  const value = parseInt(document.getElementById('id1').value, 10);

  if (isNaN(value)) {

    addDiv();

    addDiv2();

    addDiv3();

    addbutterfly();

    incrementValue();

  } else {

    incrementValue();

    // Get the value of the "textfield"

    const value = parseInt(document.getElementById('id1').value, 10);

    // If the value is even, add "bfly2.gif", otherwhise add "bfly1.gif"

    document.getElementById("bfly").src = value % 2 === 0 ? "bfly2.gif" : "bfly1.gif";

  }

})


function addDiv() {

  var div1 = document.createElement('div');

  div1.classList.add('div1');

  document.body.appendChild(div1);

}


function addDiv2() {

  var div2 = document.createElement('div');

  div2.classList.add('div2');

  document.querySelector(".div1").appendChild(div2);

}


function addDiv3() {

  var div3 = document.createElement('div');

  div3.classList.add('div3');

  document.querySelector(".div2").appendChild(div3);

}


function addbutterfly() {

  var img = document.createElement('img');

  img.id="bfly"

  img.src = "bfly1.gif";

  document.querySelector(".div3").appendChild(img);

}



function incrementValue() {

  var value = parseInt(document.getElementById('id1').value, 10);

  value = isNaN(value) ? 0 : value;

  value++;

  document.getElementById('id1').value = value;

}

.div1 {

  background-color: red;

  margin: 1em;

  height: 500px;

  width: 500px;

  justify-content: center;

  align-items: center;

}


.div2 {

  background-color: yellow;

  height: 300px;

  width: 300px;

}


.div3 {

  background-color: limegreen;

  height: 150px;

  width: 150px;

}


div {

  display: flex;

  justify-content: center;

  align-items: center;

  position: relative;

}

<html>


<head>

  <meta charset="utf-8">

  <script src="bflyjs.js" defer></script>

</head>


<body>

  <div class="button">

    <button type="button" id="add">click to get nested divs</button>

    <textarea id="id1"></textarea>

  </div>

</body>


</html>


查看完整回答
反對 回復 2023-07-14
  • 1 回答
  • 0 關注
  • 137 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號