简体   繁体   English

使带有图像的锚点标签可点击

[英]Make anchor tag with image clickable

I am using one plugin that one is http://www.menucool.com/slider/thumbnail-slider-demo-2 . 我使用的一个插件是http://www.menucool.com/slider/thumbnail-slider-demo-2 But i want to implement clickable image. 但我想实现可点击的图像。

In this plugin show image by anchor tag like this: 在此插件中,通过锚标记显示图像,如下所示:

<div style="padding:120px 0;">
        <div id="thumbnail-slider">
            <div class="inner">
                <ul>
                    <li>
                        <a class="thumb" href="img/6.jpg"></a>
                    </li>
                    <li>
                        <a class="thumb" href="img/7.jpg"></a>
                    </li>
               </ul>
          </div>
       </div>
  </div>  

I want to redirect page after click on this image. 单击此图像后,我想重定向页面。 Give me some idea about this. 给我一些想法。 It is possible to Make anchor tag with image clickable with this plugin? 可以通过此插件使带有图像的锚点标签可点击吗? Give me some idea. 给我一些想法。
Thanks 谢谢

Instead of using 而不是使用

<a class="thumb" href="img/6.jpg"></a>

you can use 您可以使用

<a class="thumb" href="img/6.jpg"><img src="img/6.jpg" alt="" /></a>

In anchor tag, you can also change the href attribute if you want to redirect it to any url, like this: 在锚标记中,如果要将其重定向到任何URL,也可以更改href属性,如下所示:

 <a class="thumb" href="http://www.google.com"><img src="img/6.jpg" alt="" /></a>

If I understand you correctly, you just need to listen to the click event, then do whatever you want. 如果我对您的理解正确,则只需要听click事件,然后做任何您想做的事情即可。

In the below demo, if you click on the front slide it will redirect to google. 在下面的演示中,如果您单击前幻灯片,它将重定向到google。 If not, it will do nothing. 如果没有,它将什么都不做。 If you want to redirect when click on any image, just remove the if statement that check that the image in the front: 如果要在click任何图像时进行重定向,只需删除if语句,以检查图像是否位于前面:

Note: It will not do the redirect because of security reason so you can see it in this bin . 注意:由于安全原因,它不会进行重定向,因此您可以在此bin中看到它。

 [].forEach.call(document.querySelectorAll('.thumb'), function(a) { a.addEventListener('click', function(event) { // Check if the image in the front if (this.parentNode.classList.contains('active')) { location.href = this.dataset.href; } }); }); 
 <link href="http://www.menucool.com/slider/thumb/2/thumbnail-slider.css" rel="stylesheet" /> <script src="http://www.menucool.com/slider/thumb/2/thumbnail-slider.js"></script> <div id="thumbnail-slider"> <div class="inner"> <ul> <li> <a class="thumb" href="http://www.menucool.com/slider/thumb/img/6.jpg" data-href="http://google.com"></a> </li> <li> <a class="thumb" href="http://www.menucool.com/slider/thumb/img/7.jpg" data-href="http://facebook.com"></a> </li> <li> <a class="thumb" href="http://www.menucool.com/slider/thumb/img/2.jpg" data-href="http://facebook.com"></a> </li> <li> <a class="thumb" href="http://www.menucool.com/slider/thumb/img/3.jpg" data-href="http://facebook.com"></a> </li> <li> <a class="thumb" href="http://www.menucool.com/slider/thumb/img/4.jpg" data-href="http://facebook.com"></a> </li> <li> <a class="thumb" href="http://www.menucool.com/slider/thumb/img/5.jpg" data-href="http://facebook.com"></a> </li> <li> <a class="thumb" href="http://www.menucool.com/slider/thumb/img/8.jpg" data-href="http://facebook.com"></a> </li> <li> <a class="thumb" href="http://www.menucool.com/slider/thumb/img/9.jpg" data-href="http://facebook.com"></a> </li> <li> <a class="thumb" href="http://www.menucool.com/slider/thumb/img/10.jpg" data-href="http://facebook.com"></a> </li> <li> <a class="thumb" href="http://www.menucool.com/slider/thumb/img/11.jpg" data-href="http://facebook.com"></a> </li> </ul> </div> </div> 

http://jsbin.com/vikejo/2/edit?html,js,output http://jsbin.com/vikejo/2/edit?html,js,output

Edit To redirect to a different URL for each image, you can add a data- attribute, in this case data-href , then, when user clicks on the image he redirect to the URL which is the value of the attribute. 编辑要重定向到每个图像的不同URL,可以添加一个data-属性,在本例中为data-href ,然后,当用户单击该图像时,他将重定向到该URL,该URL是该属性的值。

The link 链接

<a class="thumb" href="http://www.menucool.com/slider/thumb/img/6.jpg" data-href="http://google.com"></a>

In the click event 在点击事件中

location.href = this.dataset.href;

use this 用这个

<li><a href="/"><img class="thumb" src="1.jpg" style="height:auto;" /></a></li>

source :- http://www.menucool.com/jquery-slider 来源: -http : //www.menucool.com/jquery-slider

<div style="padding:120px 0;">
    <div id="thumbnail-slider">
        <div class="inner">
            <ul>
                <li>
                    <a class="thumb" href="#target_url"><img src="img/6.jpg"></a>
                </li>
                <li>
                    <a class="thumb" href="#target_url"><img src="img/7.jpg"></a>
                </li>
           </ul>
      </div>
   </div>

Use image tag inside a tag to display image as hyperlink 在标签内使用图片标签将图片显示为超链接

<a class="thumb" href="#target_url"><img src="img/6.jpg"></a>

这是简单明了的答案

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

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