开发者

JQuery image wrap and toggle

I have this code blow, it is use to switch an image to a second image on mouse click and also wrap a link around it at the same time. Upon clicking the image the second time it will take you to the link and also toggle back to the previous image.

What I'm trying to work out is how to also when it toggles back to the previous image is to also get rid of the wrapped link.

Also would it be possible to use the toggle on mouse click on another object. for example clicking a div on the page and it will toggle the image back to the original.

jQuery(function() {

    var largeLink = "<a href='index.html' target='_blank' ></a>";
    var largeLink2 = "<a href='#'></a>";
    $(".buyimage").live('click', function() {
        if ($(this).attr("class") == "buyimage") {
            this.src = this.src.replace("_5.99", "_buynow");
        } else {
            this.src = this.src.replace("_buynow", "_5.99");
        }

        $(".buyimage开发者_如何学编程").wrap(largeLink);

        $(this).toggleClass("on")
    });
});


You can use the jquery unwrap:

http://api.jquery.com/unwrap/

// for example
<script>
  $("button").toggle(function(){
    $("p").wrap("<div></div>");
  }, function(){
    $("p").unwrap();
  });
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜