简体   繁体   English

将jQuery添加到js代码

[英]Adding jQuery to js code

I wanted to make this statement using jQuery but cant quite get it to work. 我想使用jQuery声明此语句,但无法使其正常工作。 In this statement I am clicking on an image in my html and changing it to a new image (img/HeaderImg2.jpg) Please help. 在此声明中,我单击html中的图像并将其更改为新图像(img / HeaderImg2.jpg),请帮忙。

  var imageButton = document.getElementById('headerImg');
  imageButton.addEventListener('click', function(e)
  {
   console.log(e);
   imageButton.src ='img/HeaderImg2.jpg';
  } ,false);

Try this: 尝试这个:

$('#headerImg').on('click',function(e) {
    console.log(e);
    $(this).attr('src','img/HeaderImg2.jpg');
});

Btw, it's better to use vanilla javascript other than using jQuery if you're able to do so. 顺便说一句,如果可以的话,最好使用香草javascript,而不是使用jQuery。

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

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