具有img alt属性的jQuery或Javascript onclick [英] JQuery or Javascript onclick with img alt attribute

查看:89
本文介绍了具有img alt属性的jQuery或Javascript onclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将图像包装在带有类的div中,并且图像具有'alt'和'width'属性.

I have the image which is wrapped in a div with a class and image has 'alt' and 'width' attributes.

我正在使用alt属性将图像与其他图像区分开.其他图像也位于相同的div和class中. 我想单击选择的图像,单击后应带我到URL为"www.google.com"的网页.

I'm using alt attribute to distinguish the image from other images. Other images are also in the same div and class. I like to click on the image I select and after click it should take me to the webpage whose url is "www.google.com".

如何使用alt属性绑定到click事件并实现此逻辑?

How I can bind to the click event using alt attribute and implement this logic?

是否可以使用JQuery或Javascript做到这一点?

Is it possible to do so using JQuery or Javascript?

这是我的代码,但是不起作用.我该如何解决?

Here is my code, but it is not working. How can I fix this?

$('img[alt="Example"]').on("click", "a,img", function (e) {
      e.preventDefault();
      document.location = 'www.google.com';
  });

推荐答案

    $(document).ready(function() {
        $('img.thumbnail').click(function() {
            console.log();
            window.location.href = this.alt;
        });
    });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img src="bottle.jpg" alt='https://www.google.co.in' src="http://placehold.it/150x150" class="thumbnail" id="bottle" />
<img src="glass.jpg" alt='https://www.google.co.in' src="http://placehold.it/150x150" class="thumbnail" id="glass" />

这篇关于具有img alt属性的jQuery或Javascript onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆