简体   繁体   English

如何将此 JQuery 转换为香草 Javascript?

[英]How can I convert this JQuery into vanilla Javascript?

hello there i have been trying for the last few day to convert all my jquery into vanilla javascript and this is one of the few that i am having the problems with你好,过去几天我一直在尝试将我所有的 jquery 转换为香草 javascript,这是我遇到问题的少数几个之一

 $(".book").click(function(){
    var timeslot = $(this).attr('data-timeslot');
    $("#slot").html(timeslot);
    $("#timeslot").val(timeslot);

})

this is what i have come up with so far and dont even know if i have started converting this correctly这是我到目前为止想出的,甚至不知道我是否已经开始正确转换

var book = document.querySelector(".book");
book.addEventListener('click', function() {

  var timeslot = book.getAttribute('data-timeslot');
  document.getElementById("slot").innerHTML = timeslot;


}());

question is am i along the right lines or am heading the wrong direction问题是我是沿着正确的路线还是朝着错误的方向前进

it is almost correct, except select should take all matches这几乎是正确的,除了 select 应该接受所有匹配项

var books = document.querySelectorAll(".book");
[...books].forEach(book => {
   book.addEventListener('click', function() {

   var timeslot = book.getAttribute('data-timeslot');
   document.getElementById("slot").innerHTML = timeslot;
  });
})

 var books = document.getElementsByClassName("book"); books.forEach( function(book) { book.addEventListener('click', function() { var timeslot = book.getAttribute('data-timeslot'); document.getElementById("slot").innerHTML = timeslot; }); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM