简体   繁体   English

使用jQuery切换图像

[英]toggle image using jquery

I am having an img inside <p> tag and if I click the img or the <p> it needs to be toggled and the img should changed . 我在<p>标记内有一个img ,如果click img<p>则需要对其进行toggled并且应该changed img。

I have done it by adding jQuery(this).attr("src", "img/hide.png"); 我通过adding jQuery(this).attr("src", "img/hide.png"); but the img is not changing can any one suggest me the right path. 但img不变,任何人都可以建议我正确的道路。

Here is my script: 这是我的脚本:

<script type="text/javascript">
    jQuery(document).ready(function () {
        jQuery(".content").hide();
        //toggle the componenet with class msg_body
        jQuery(".heading").click(function () {
            jQuery(this).next(".content").slideToggle(500);
        });
      });
</script>

This is my CSS: 这是我的CSS:

.layer1 {
margin: -5px;
padding: 0px;
width: 860px;
}

This is my markup: 这是我的标记:

<div class="layer1">
<p class="heading"><img src="img/show.png" alt="" style="margin:0 5px 0 0;" /></p>
<div class="content"></div>
<p class="heading"><img src="img/show.png" alt="" style="margin:0 5px 0 0;" /></p>
<div class="content"></div>
<p class="heading"><img src="img/show.png" alt="" style="margin:0 5px 0 0;" /></p>
<div class="content"></div>
<p class="heading"><img src="img/show.png" alt="" style="margin:0 5px 0 0;" /></p>
<div class="content"></div>
<p class="heading"><img src="img/show.png" alt="" style="margin:0 5px 0 0;" /></p>
<div class="content"></div>
</div>

jQuery(this).attr("src", "img/hide.png");

In this context this will refer to the .heading (the p tag). 在这种情况下, this将指向.headingp标记)。 You'll have to dig down a little deeper to reference the image: 您必须更深入地挖掘以引用图像:

jQuery(this).find('img').attr("src", "img/hide.png");

Here's a jsFiddle to show: 这是一个显示的jsFiddle:

http://jsfiddle.net/jonathon/yBFWM/ http://jsfiddle.net/jonathon/yBFWM/


Edit I missed the content toggling bit of the question. 编辑我错过了问题的内容切换。

I've added a bit so that it will know what the 'active' content area is. 我添加了一些内容,以便它知道什么是“活动”内容区域。 Then when you click an image, it will hide the active content area and display the current. 然后,当您单击图像时,它将隐藏活动内容区域并显示当前内容。 I also added a bit so the click handler wont toggle the content if it's already selected. 我还添加了一些内容,以便单击处理程序不会切换已选择的内容。

jQuery(".heading").click(function() {
    var $this = $(this),
        $contentArea = $this.next('.content');

    if (!$contentArea.hasClass('active')) {
        $(".content.active").slideToggle(500).removeClass('active');
        $contentArea.slideToggle(500).addClass("active");

        $this.find('img').attr("src", "http://www.google.com/images/icons/ui/doodle_plus/doodle_plus_google_logo_on_grey.gif");
    }
});

Did you include the line jQuery(this).attr("src", "img/show.png") in the click-handler defined by jQuery(".heading").click() ? 您是否在由jQuery(".heading").click()定义的点击处理程序中添加了jQuery(this).attr("src", "img/show.png") jQuery(".heading").click() In this case, your code cannot work because this refers to the <p class="heading"> , not to the image. 在这种情况下,您的代码将无法工作,因为this引用了<p class="heading"> ,而不是图像。

Simply change the line to 只需将行更改为

jQuery('img',this).attr("src", "img/show.png")
$("p").mousedown(function(){
    $(this).find('img').attr('src','img/img1.png');
})

$("p").mouseup(function(){
    $(this).find('img').attr('src','img/img2.png');
})

$("img").mousedown(function(){
    $(this).attr('src','img/img1.png');
})

$("img").mouseup(function(){
    $(this).attr('src','img/img2.png');
})
<script type="text/javascript">
    jQuery(document).ready(function () {
        var imgShow = 'img/show.png';
        var imgHide = 'img/hide.png';
        jQuery(".content").hide();
        //toggle the componenet with class msg_body
        jQuery(".heading").click(function () {
            jQuery(this).next(".content").slideToggle(500);
            if (JQuery(this).find("img").attr('src') == imgShow ) {
                JQuery(this).find("img").attr('src', imgHide);
            } else {
                JQuery(this).find("img").attr('src',imgShow);
            }
        });
      });
</script>

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

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