使用 jquery 从锚点 onclick 更改图像 src?

发布于 2024-09-30 11:41:33 字数 1187 浏览 1 评论 0原文

当我单击图像周围的链接时,我使用 jquery 弹出 AddThis 共享框。效果很好。我正在使用的 jquery 代码在这里:

        //added to show/hide add-this
    $('a#slick-toggle').click(function() {
        $('#atBox').toggle(100);
        return false;
    });

这是我的相关 html:

<a id="slick-toggle" href="#" title="Share">
<img src="images/navicon/navicon4_off.gif" alt="share" id="share" class="img-swap">
</a>

这使得出现一个小 div 框,其中有一些共享图标。它正在按预期工作。

我想做的也是在单击锚标记时使锚标记内的图像“切换”。不幸的是,我发现执行此操作的常规 jquery 图像“onclick”脚本似乎不起作用,大概是因为单击了“链接”,而不是图像?不管怎样,我尝试通过执行以下操作将初始脚本与图像交换脚本“合并”,但没有运气:

     $('a#slick-toggle').click(function() {

        if ($('#share').attr("class") == "img-swap") {
            this.src = this.src.replace("_off","_on");
        } else {
            this.src = this.src.replace("_on","_off");
        }
        $('.img-swap').toggleClass("on");


        $('#atBox').toggle(100);
        return false;
    });

我在第三行中放入的内容似乎并不重要,我无法让 jquery 识别出这一点我指的是图像。我不断收到“无法调用未定义的方法‘替换’”消息。我已经尝试过 $('#share') 和 ('#share'),它们都给了我相同的消息。我对 jquery 不太熟悉,但是当在锚点 onclick 事件上调用函数时,我是否在脚本中做了一些明显错误的事情来引用图像标签?

谢谢!

I'm using jquery to pop up an AddThis share box when I click on a link that surrounds an image. It works great. The jquery code I'm using is here:

        //added to show/hide add-this
    $('a#slick-toggle').click(function() {
        $('#atBox').toggle(100);
        return false;
    });

And this is my relevant html:

<a id="slick-toggle" href="#" title="Share">
<img src="images/navicon/navicon4_off.gif" alt="share" id="share" class="img-swap">
</a>

This makes a little div box appear that has some sharing icons. It's working as intended.

What I wanted to do was also make the image that is inside the anchor tags "switch" when the anchor tag was clicked on. Unfortunately the regular jquery image "onclick" scripts I found for doing that don't seem to work, presumably because the "link" is being clicked, not the image? Anyway, I tried "merging" the initial script with an image swapping script by doing the following, but no luck:

     $('a#slick-toggle').click(function() {

        if ($('#share').attr("class") == "img-swap") {
            this.src = this.src.replace("_off","_on");
        } else {
            this.src = this.src.replace("_on","_off");
        }
        $('.img-swap').toggleClass("on");


        $('#atBox').toggle(100);
        return false;
    });

It doesn't seem to matter what I put in that third line, I can't ever get the jquery to recognize that I'm referring to the image. I keep getting a "Cannot call method 'replace' of undefined" message. I've tried this, $('#share') and ('#share'), and they all give me the same message. I'm not too familiar with jquery but am I doing something obviously wrong in my script to refer to the image tag when the function is being called on the anchor onclick event?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好久不见√ 2024-10-07 11:41:33

您需要找到里面的图像,因为您的 click 处理程序位于包装它的 上,如下所示:

$('a#slick-toggle').click(function() {
  var img = $(this).find("img")[0];
    if ($('#share').attr("class") == "img-swap") {
        img.src = img.src.replace("_off","_on");
    } else {
        img.src = img.src.replace("_on","_off");
    }
    $('.img-swap').toggleClass("on");
    $('#atBox').toggle(100);
    return false;
});

或者,像这样切换一下:

$('a#slick-toggle').click(function() {
  var img = $('#share')[0], isSwap = img.className == "img-swap";
  img.src = isSwap ? img.src.replace("_off","_on") : img.src.replace("_on","_off");
  $('.img-swap').toggleClass("on");
  $('#atBox').toggle(100);
  return false;
});

You need to find the image inside since your click handler is on the <a> that wraps it, like this:

$('a#slick-toggle').click(function() {
  var img = $(this).find("img")[0];
    if ($('#share').attr("class") == "img-swap") {
        img.src = img.src.replace("_off","_on");
    } else {
        img.src = img.src.replace("_on","_off");
    }
    $('.img-swap').toggleClass("on");
    $('#atBox').toggle(100);
    return false;
});

Or, switch things up a bit like this:

$('a#slick-toggle').click(function() {
  var img = $('#share')[0], isSwap = img.className == "img-swap";
  img.src = isSwap ? img.src.replace("_off","_on") : img.src.replace("_on","_off");
  $('.img-swap').toggleClass("on");
  $('#atBox').toggle(100);
  return false;
});
北音执念 2024-10-07 11:41:33

另一种选择:尝试使用 .children() 选择您单击的当前元素的子元素

$(this).children('> img')

在这种情况下,图像是调用单击函数的位置的直接后代。

http://api.jquery.com/children/

Another alternative: try using .children() to select a child element of the current one you've clicked

$(this).children('> img')

In that case, the image that is a direct descendant of where the click function is called.

http://api.jquery.com/children/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文