简体   繁体   English

jQuery函数attr()并不总是更新img src属性

[英]jQuery function attr() doesn't always update img src attribute

Context: On my product website I have a link for a Java webstart application (in several locations). 上下文:在我的产品网站上,我有一个Java Webstart应用程序的链接(在几个位置)。

My goal: prevent users from double-clicking, ie only "fire" on first click, wait 3 secs before enabling the link again. 我的目标是:防止用户双击,即仅在第一次单击时“触发”,请等待3秒钟,然后再次启用链接。 On clicking, change the link image to something that signifies that the application is launching. 单击后,将链接图像更改为表示该应用程序正在启动的内容。

My solution works, except the image doesn't update reliably after clicking. 我的解决方案有效,除了单击后图像无法可靠更新。 The commented out debug output gives me the right content and the mouseover callbacks work correctly, too. 注释掉的调试输出为我提供了正确的内容,并且mouseover回调也正常工作。

See it running here: http://www.auctober.de/beta/ (click the Button "jetzt starten"). 看到它在这里运行: http ://www.auctober.de/beta/(单击“ jetzt starten”按钮)。

BTW: if anybody has a better way of calling a function with a delay than that dummy-animate, let me know. 顺便说一句:如果有人比那个虚拟人有更好的延迟调用函数的方法,请告诉我。

JavaScript: JavaScript:

<script type="text/javascript">
      <!--
        allowClick = true;
        linkElements = "a[href='http://www.auctober.de/beta/?startjnlp=true&rand=1249026819']";
        $(document).ready(function() {
            $('#jnlpLink').mouseover(function() {
                if ( allowClick ) {
                    setImage('images/jetzt_starten2.gif');
                }
            });
            $('#jnlpLink').mouseout(function() {
                if ( allowClick ) {
                    setImage('images/jetzt_starten.gif');
                }
            });

           $(linkElements).click(function(evt) {
                if ( ! allowClick ) {
                    evt.preventDefault();
                }
                else {
                    setAllowClick(false);
                    var altContent = $('#jnlpLink').attr('altContent');
                    var oldContent = $('#launchImg').attr('src');
                    setImage(altContent);
                    $(this).animate({opacity: 1.0}, 3000, "", function() {
                        setAllowClick(true);
                        setImage(oldContent);
                    });
                }
            });

        });

        function setAllowClick(flag) {
            allowClick = flag;
        }
        function setImage(imgSrc) {
            //$('#debug').html("img:"+imgSrc);
            $('#launchImg').attr('src', imgSrc);
        }
      //-->
</script>

A delay can be achieved with the setTimeout function 通过setTimeout函数可以实现延迟

setTimeout(function() { alert('something')}, 3000);//3 secs

And for your src problem, try: 对于您的src问题,请尝试:

$('#launchImg')[0].src = imgSrc;

Check out the BlockUI plug-in. BlockUI插件。 Sounds like it could be what you're looking for. 听起来可能是您要找的东西。

You'll find a nice demo here . 您会在这里找到一个不错的演示。

...or just use: ...或仅使用:

$(this).animate({opacity: '1'}, 1000);

wherever you want in your code, where $(this) is something that is already at opacity=1...which means everything seemingly pauses for one second. $ {this)是您已经在opacity = 1的位置的代码,这意味着一切似乎都暂停了一秒钟。 I use this all the time. 我经常用这个。

Add this variable at the top of your script: 在脚本顶部添加此变量:

var timer;

Implement this function: 实现此功能:

function setFlagAndImage(flag) {
    setAllowClick(flag);
    setImage();
}

And then replace the dummy animation with: 然后将虚拟动画替换为:

timer = window.setTimeout(function() { setFlagAndImage(true); }, 3000);

If something else then happens and you want to stop the timer, you can just call: 如果随后发生其他事情,并且您想停止计时器,则可以致电:

window.clearTimeout(timer);

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

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