第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

事件委托幫助,我如何才能僅使用純 Javascript 定位 <img> 標(biāo)簽?

事件委托幫助,我如何才能僅使用純 Javascript 定位 <img> 標(biāo)簽?

心有法竹 2023-09-07 17:13:55
我正在學(xué)習(xí)事件委托,我將點擊事件放在主父級上,即縮略圖。當(dāng)我單擊底部的圖像時,我想將 src 設(shè)置為 img class="main" src,但我注意到當(dāng)我在圖像之間單擊時,單擊縮略圖 div 上的其他任何位置,或者按 Tab 然后輸入我得到空。我查看了一下,發(fā)現(xiàn)我的 event.target 目標(biāo)是錨標(biāo)記以及 div 類“縮略圖”。我明白為什么,當(dāng)我將點擊事件放在縮略圖(父級)上時,但是有沒有辦法我只能定位 img 標(biāo)簽?請不要使用 jQuery,我想使用純 js 來學(xué)習(xí)這個,謝謝!function displaySelected() {  const getMainClass = document.querySelector('.main');  const getThumbnails = document.querySelector('.thumbnails');  getThumbnails.addEventListener('click', function(event) {    getMainClass.setAttribute('src', event.target.getAttribute('src'));    console.log(event.target);  });}displaySelected();* {  box-sizing: border-box;}main {  min-width: 250px;  padding: 30px;}.hero {  height: 200px;  margin-bottom: 20px;}.hero img {  height: 100%}.thumbnail {  display: inline-block;  cursor: pointer;  height: 60px;}a.thumbnail:hover {  box-shadow: 5px 5px 5px gray;}.thumbnail img {  max-height: 100%;}<!DOCTYPE html><html><head>  <meta charset="UTF-8">  <meta name="viewport" content="width=device-width, initial-scale=1.0">  <link rel="stylesheet" href="style.css">  <title>Document</title></head><body>  <main role="main">    <h1>Image Carousel</h1>    <div class="hero">      <img class="main" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-65/cat1.jpg" alt="An orange-eyed grey cat stretches toward the camera."/>    </div>    <div class="thumbnails">      <a class="thumbnail" href="#"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-65/cat1.jpg" alt="An orange-eyed grey cat stretches toward the camera."/></a>      <a class="thumbnail" href="#"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-65/cat2.jpg" alt="Closeup of a blue-eyed, grey cat with markings."/></a>      <a class="thumbnail" href="#"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-65/cat3.jpg" alt="An orange cat licks its paw."/></a>      <a class="thumbnail" href="#"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-65/cat4.jpg" alt="A content brown cat lounges with eyes closed."/></a>    </div>
查看完整描述

1 回答

?
慕勒3428872

TA貢獻1848條經(jīng)驗 獲得超6個贊

event listener您每次調(diào)用時都會添加function。您應(yīng)該只添加event listener一次。


檢查tagName是否target為 an IMG,如果是,則設(shè)置src,否則不執(zhí)行任何操作


//Add the event listener once the DOM loads

const getThumbnails = document.querySelector('.thumbnails');

getThumbnails.addEventListener('click', function(event) {

    displaySelected(event);

});



//Check if the event target has tagName of IMG

function displaySelected(event) {

    if(event.target.tagName === "IMG"){

        const getMainClass = document.querySelector('img.main');

        getMainClass.src = event.target.getAttribute('src');

    }

}

{

  box-sizing: border-box;

}


main {

  min-width: 250px;

  padding: 30px;

}


.hero {

  height: 200px;

  margin-bottom: 20px;

}


.hero img {

  height: 100%

}



.thumbnail {

  display: inline-block;

  cursor: pointer;

  height: 60px;

}


a.thumbnail:hover {

  box-shadow: 5px 5px 5px gray;

}


.thumbnail img {

  max-height: 100%;

}

<!DOCTYPE html>

<html>

<head>

  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <link rel="stylesheet" href="style.css">

  <title>Document</title>

</head>

<body>


  <main role="main">

    <h1>Image Carousel</h1>

    <div class="hero">

      <img class="main" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-65/cat1.jpg" alt="An orange-eyed grey cat stretches toward the camera."/>

    </div>

    <div class="thumbnails">

      <a class="thumbnail" href="#"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-65/cat1.jpg" alt="An orange-eyed grey cat stretches toward the camera."/></a>

      <a class="thumbnail" href="#"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-65/cat2.jpg" alt="Closeup of a blue-eyed, grey cat with markings."/></a>

      <a class="thumbnail" href="#"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-65/cat3.jpg" alt="An orange cat licks its paw."/></a>

      <a class="thumbnail" href="#"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-65/cat4.jpg" alt="A content brown cat lounges with eyes closed."/></a>

    </div>

  </main>

</body>

</html>


查看完整回答
反對 回復(fù) 2023-09-07
  • 1 回答
  • 0 關(guān)注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號