简体   繁体   English

JQuery中的超链接仅在JSFiddle中有效

[英]Hyperlink in JQuery Only Working in JSFiddle

I found something that can not only toggle on/off an image, but also make that image a link. 我发现了一些东西,不仅可以打开/关闭图像,还可以使该图像成为链接。

Problem: It only works in JSFiddle. 问题:它仅在JSFiddle中有效。

I put everything back into html (providing script) and made sure that everything was the same...but still, on my site it won't work. 我将所有内容放回html(提供脚本)中,并确保所有内容都相同...但是,在我的网站上,它仍然无法正常工作。 On JSFiddle, it does. 在JSFiddle上,它可以。

If anyone has a solution, I'd be most grateful. 如果有人能解决,我将不胜感激。

The code I'm using for the site: 我用于网站的代码:

<center>
    <p>
       <div class="icon-container">
          <a id="TOURBUTTON">
              <img src="http://static.tumblr.com/6s0fefr/vFQn5uj2h/tournew.png" style="width: 188px; height: 188px;" />
          </a>
        </div>
    </p>
</center>   
<center>
    <p>
        <div class="display-container">
            <img id="T5" style="display:none;" a href="http://music.britrodriguez.com" src="http://static.tumblr.com/6s0fefr/GXHnabnep/tahoeshow.png"/>
            <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
            <script>
                $(document).ready(function(){
                      $('#TOURBUTTON').on("click", function(){
                          $('#T5').toggle();
                      });  
                });

                $('#T5').click(function(event){
                    var link = $(this);
                    var target = link.attr("target");
                    if ($.trim(target).length > 0){
                        window.open(link.attr("href"), target);
                    } else {
                        window.location = link.attr("href");
                    }
                    event.preventDefault();
                });
            </script>
            <style type="text/css">
                .icon-container{
                    display:inline-block;
                    margin-bottom: 0px;
                    margin-top: 10px;
                }
            </style>

The JSFiddle: JSFiddle:

http://jsfiddle.net/ccymzmvn/ http://jsfiddle.net/ccymzmvn/

The site it's not working on: 它不在的网站:

http://www.britrodriguez.com/HITEST http://www.britrodriguez.com/HITEST

Why do you open the url with JavaScript? 为什么使用JavaScript打开网址? Just try: 你试一试:

<a href="http://music.britrodriguez.com">
    <img src="http://static.tumblr.com/6s0fefr/GXHnabnep/tahoeshow.png" />
</a>

These are just suggestions, but: 这些只是建议,但是:

  1. Make sure your HTML document is well formed and remove extraneous levels. 确保您的HTML文档格式正确,并删除无关的级别。 The deeper the DOM tree goes, the "heavier" the page can get for the browser. DOM树越深,页面就可以为浏览器“加重”。 Always strive towards a shallow DOM tree 总是努力向浅的DOM树迈进
  2. The event handler when you click #T5 doesn't really need jQuery, I've used native JS, you can see it has a one to one drop-in. 当您单击#T5时,事件处理程序实际上并不需要jQuery,我使用了本机JS,您可以看到它具有一对一的插件。
  3. Whenever you have a click event on an element, change the cursor for the user so they know it is clickable. 每当元素上有click事件时,请更改用户的光标,使他们知道它是可单击的。
  4. I have also user opacity to hide the #T5 instead of display. 我也有用户不透明隐藏#T5而不是显示。 That way you can make it fade nicely 这样你就可以使其褪色

http://jsfiddle.net/ccymzmvn/5/ http://jsfiddle.net/ccymzmvn/5/

HTML 的HTML

<p class="icon-container">
    <a id="TOURBUTTON">
        <img src="http://static.tumblr.com/6s0fefr/vFQn5uj2h/tournew.png" />
    </a>
</p>

<p class="display-container">
    <a href="http://music.britrodriguez.com">
        <img id="T5" src="http://static.tumblr.com/6s0fefr/GXHnabnep/tahoeshow.png" />
    </a>
</p>

CSS 的CSS

body {
    text-align: center;
}

#TOURBUTTON {
    display: inline-block;
}

#TOURBUTTON img {
    cursor: pointer;
    display: block;
    width: 188px;
    height: 188px;
}

img#T5 {
    border: 1px solid red;
    max-width: 100%;
    opacity: 0;
    pointer-events: none;
    -webkit-transition: opacity 800ms;
    transition: opacity 800ms;
}

img#T5.active {
    opacity: 1;
    pointer-events: auto;
}

JavaScript 的JavaScript

function open_link(event) {
    event.preventDefault();

    var link = this,
        target = link.target;

    if($.trim(target).length > 0) {
        window.open(link.href, target);
    } else {
        window.location = link.href;
    }

}


$(document).ready(function() {
    var $T5 = $('#T5');

    $('#TOURBUTTON').on("click", function(){
        $T5.toggleClass('active');
    });  

    $T5.on('click', open_link);
});

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

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