你好,最近几天我一直在尝试将我的所有jquery转换为普通javascript,这是我遇到问题的少数几个
$(".book").click(function(){
var timeslot = $(this).attr('data-timeslot');
$("#slot").html(timeslot);
$("#timeslot").val(timeslot);
})
这是我到目前为止提出的,甚至都不知道我是否已经开始正确转换了
var book = document.querySelector(".book");
book.addEventListener('click', function() {
var timeslot = book.getAttribute('data-timeslot');
document.getElementById("slot").innerHTML = timeslot;
}());
问题是我沿着正确的路线还是走错了方向
答案 0 :(得分:1)
这几乎是正确的,除了select应该参加所有比赛
var books = document.querySelectorAll(".book");
[...books].forEach(book => {
book.addEventListener('click', function() {
var timeslot = book.getAttribute('data-timeslot');
document.getElementById("slot").innerHTML = timeslot;
});
})
答案 1 :(得分:0)
var books = document.getElementsByClassName("book");
books.forEach( function(book) {
book.addEventListener('click', function() {
var timeslot = book.getAttribute('data-timeslot');
document.getElementById("slot").innerHTML = timeslot;
});
});