gpt4 book ai didi

javascript - 如何让这个div可以点击?

转载 作者:太空宇宙 更新时间:2023-11-03 21:24:22 25 4
gpt4 key购买 nike

我想让这个 wordpress 插件的每张“卡片”都可以点击 ( http://www.davidbo.dreamhosters.com/?page_id=11 )

所以我添加了一个纯 JS 元素,代码如下:

document.getElementsByClassName('fc_card-container').onclick = function() {alert('It works!');}

它不起作用,所以我想知道这是怎么回事。

谢谢!

最佳答案

如果您想为所有卡片应用点击事件处理程序:

// Get all the elements with a .fc_card-container class and store them on a variable
// .getElementsByClassName returns an array-like object with all the selected elements
var cards = document.getElementsByClassName('fc_card-container');

// Use [].slice.apply(cards) to convert the cards NodeList into an array
// Iterate over the cards array with a .forEach

[].slice.apply(cards).forEach(function(card, index){

// Each array element corresponds to a card element
// Use the .addEventListener( EVENT, callback ) to attach an event handler for each card element

card.addEventListener("click", function(e){
alert();
console.log(cards[index]); // Index gives us the exact array position (index) of the clicked card.
console.log(e.target); // e.target gives us access to the clicked element
});

});

关于javascript - 如何让这个div可以点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34487615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com