如何一次在多个标签上添加事件监听器

时间:2018-10-23 14:06:43

标签: javascript html javascript-events event-handling addeventlistener

我是Java语言的新手。而且我想在main-img上替换此图像,以下代码可以正常工作,但我只想知道如何通过使用javascript中更少的代码

<div id="container">
        <div id="side-img">
            <img id="side1" onclick="side1()" src="img1.jpeg">
            <img id="side2" onclick="side2()" src="img2.jpeg">
            <img id="side3" onclick="side3()" src="img3.jpeg">
            <img id="side4" onclick="side4()" src="img4.jpeg">
        </div>
        <div id="main-img">
            <img id="main" src="img0.jpeg">
        </div>

    </div>




    <script type="text/javascript">
        var sideimg = document.querySelectorAll('#side-img img');
        var main = document.querySelector('#main');

             function side1() {
                main.src = sideimg[0].src;
             }
             function side2() {
                main.src = sideimg[1].src;
             }
             function side3() {
                main.src = sideimg[2].src;
             }
             function side4() {
                main.src = sideimg[3].src;
             }


    </script>

4 个答案:

答案 0 :(得分:0)

您可以通过修改图像标签来简化它:

>>> list(chunked_iter(range(10), 3))
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]


并删除JS脚本中的所有side()函数并添加此函数:

adn = [
    'GAATTCCTTGAGGCCTAAATGCATCGGGGTGCTCTGGTTTTGTTGTTGTTATTTCTGAATGACATTTACTTTGGTGCTCTTTATTTTGCGTATTTAAAAC', 
    'TAAGTCCCTAAGCATATATATAATCATGAGTAGTTGTGGGGAAAATAACACCATTAAATGTACCAAAACAAAAGACCGATCACAAACACTGCCGATGTTTCTCTGGCTTAAATTAAATGTATATACAACTTATATGATAAAATACTGGGC'
    ]

result = []

for s in adn:
    result.append(list(chunked_iter(list(s), 10)))

print(result)

答案 1 :(得分:0)

您可以以编程方式为每个图像附加onclick处理程序。

document.querySelectorAll('img').forEach((img) => {
  img.onclick = () => {
    console.log(img.src)
    document.querySelector('#main').src = img.src
  }
})
<div id="container">
  <div id="side-img">
    <img id="side1" src="img1.jpeg">
    <img id="side2" src="img2.jpeg">
    <img id="side3" src="img3.jpeg">
    <img id="side4" src="img4.jpeg">
  </div>
  <div id="main-img">
    <img id="main" src="img0.jpeg">
  </div>

</div>

答案 2 :(得分:0)

我建议:

// retrieving the common ancestor element of the <img> elements:
let sideImage = document.querySelector('#side-img'),

// defining a named function, using arrow syntax; 'e' is a reference
// to the event object, passed automatically from the
// EventTarget.addEventListener() method:
  imageChange = (e) => {

    // we retrieve the element with the 'id' of 'main', and update
    // its src property to be that of the clicked element;
    // e.target retrieves the element upon which the event was
    // originally triggered:
    document.getElementById('main').src = e.target.src;
  };

sideImage.addEventListener('click', imageChange);

let sideImage = document.querySelector('#side-img'),
  imageChange = (e) => {
    document.getElementById('main').src = e.target.src;
  };

sideImage.addEventListener('click', imageChange);
<div id="container">
  <div id="side-img">
    <img id="side1" src="https://via.placeholder.com/100.png?text=image1">
    <img id="side2" src="https://via.placeholder.com/100.png?text=image2">
    <img id="side3" src="https://via.placeholder.com/100.png?text=image3">
    <img id="side4" src="https://via.placeholder.com/100.png?text=image4">
  </div>
  <div id="main-img">
    <img id="main" src="img0.jpeg">
  </div>

</div>

JS Fiddle demo

关于OP中评论中剩余的问题,

  

您能告诉我#side-img容器如何遍历每个图像并向它们添加事件侦听器吗?

当然,在这种情况下,我们利用事件委托来利用事件在DOM中冒泡的方式。

我们没有将事件侦听器绑定到多个<img>元素,而是利用事件在DOM中冒泡的方式;这意味着我们在click事件到达#side-img时监听了该事件,并查看了Event对象的target属性,以查找最初触发该事件的元素

参考文献:

答案 3 :(得分:0)

包含委托的版本

JS

const viewer = document.querySelector('#main');
document.addEventListener('click', (event) => {
    if (typeof event.target.hasAttribute('data-clickable') && event.target.src) {
        viewer.src = event.target.src;
    }
})

HTML

<div id="container">
  <div id="side-img">
    <img id="side1" data-clickable src="img1.jpeg">
    <img id="side2" data-clickable src="img2.jpeg">
    <img id="side3" data-clickable src="img3.jpeg">
    <img id="side4" data-clickable src="img4.jpeg">
  </div>
  <div id="main-img">
    <img id="main" src="img0.jpeg">
  </div>
</div>