在图像上添加类单击?

时间:2012-05-09 17:44:21

标签: jquery addclass removeclass

function updateOption(selection, option) {
        jQuery('body,html').animate({
            scrollTop: jQuery("#msg").offset().top
        }, 800);
        jQuery('.'+selection).each(function() {
            jQuery("img."+selection).removeClass("optionSelected");
        });
        jQuery.ajax({
            type: "GET",
            url: "../v1_include/v1_session_boutique.inc.php",
            data: selection + "=" + option,
            success: function(msg){
                jQuery('#msg').fadeIn('fast');
                var List = document.getElementById('msg');
                List.innerHTML = msg;
                  jQuery(".StoreMsgPanier").animate( { opacity: 0.25 }, 1000)
                    .animate( { opacity: 1 }, 1000);
            }
        });
}


<p>Option 1</p>
<div id="opt3">
    <a href="javascript:updateOption('select_1_3','1-5-1');"><img src="../v1_images_option/crop/5_1.jpg" alt="" style="margin:2px;padding:5px;border:1px solid #D6D6D6" class="select_1_3 optionSelected" /></a>
    <a href="javascript:updateOption('select_1_3','1-6-1');"><img src="../v1_images_option/crop/6_1.jpg" alt="" style="margin:2px;padding:5px;border:1px solid #D6D6D6" class="select_1_3" /></a>
</div>
<p>Option 2</p>
<div id="opt2">
    <a href="javascript:updateOption('select_1_2','1-3-1');"><img src="../v1_images_option/crop/3_1.jpg" alt="" style="margin:2px;padding:5px;border:1px solid #D6D6D6" class="select_1_2" /></a>
    <a href="javascript:updateOption('select_1_2','1-4-1');"><img src="../v1_images_option/crop/4_1.jpg" alt="" style="margin:2px;padding:5px;border:1px solid #D6D6D6" class="select_1_2" /></a>
</div>

此代码工作正常,但在删除所有'optionSelected'类后,如何将'optionSelected'仅添加到我刚刚点击的图像

感谢您的帮助......

2 个答案:

答案 0 :(得分:2)

$('#id_of_image').on('click', function() {
  $('img').removeClass('optionSelected');
  $(this).addClass('optionSelected');
});

答案 1 :(得分:2)

你可以从调用者传递this并在函数中使用它,

标记:this函数调用中添加了updateOption

    <a href="javascript:updateOption('select_1_3','1-5-1', this);">

JS:已更改为_this.find(..

  function updateOption(selection, option, _this) {
    //..
    //...
       $(_this).parent().find("img").removeClass("optionSelected"); //remove class for all images
       $(_this).find("img").addClass("optionSelected"); //add class to the image inside the link

或者,优良作法是使用不引人注目的方式..见下文,

标记:this函数调用中添加了updateOption

<p>Option 1</p>
<div id="opt3">
    <a href="javascript: void(0)" data-opt="1-5-1" data-sel="select_1_3"><img src="../v1_images_option/crop/5_1.jpg" alt="Image" class="select_1_3 img_style optionSelected" /></a>
    <a href="javascript: void(0);" data-opt="1-6-1" data-sel="select_1_3"><img src="../v1_images_option/crop/6_1.jpg" alt="Image" class="select_1_3 img_style" /></a>
</div>
<p>Option 2</p>
<div id="opt2">
    <a href="javascript: void(0)" data-opt="1-3-1" data-sel="select_1_2"><img src="../v1_images_option/crop/3_1.jpg" alt="Image" class="select_1_2 img_style" /></a>
    <a href="javascript: void(0)" data-opt="1-4-1" data-sel="select_1_2"><img src="../v1_images_option/crop/4_1.jpg" alt="Image" class="select_1_2 img_style" /></a>
</div>

<强> CSS:

.img_style { margin:2px;padding:5px;border:1px solid #D6D6D6; }

最后是JS:一堆修改,请在实现之前尝试理解它。

$(function() {
    $('.img_style').click(function() {
        $('body,html').animate({
            scrollTop: $("#msg").offset().top
        }, 800);

        var $this = $(this);        

        $this //current link
        .closest('div') //will return you the enclosing div lets say <div id="opt2">
        .find('img') //find all images inside <div id="opt2">
        .removeClass("optionSelected"); //remove class

        $this.addClass('optionSelected');

        var $parent = $this.parent();

        var option = $parent.attr('data-opt'); //will return you the option
        var selection = $parent.attr('data-sel');

        $.ajax({
            type: "GET",
            url: "../v1_include/v1_session_boutique.inc.php",
            data: selection + "=" + option,
            success: function(msg) {
                $('#msg').fadeIn('fast').html(msg);

                $(".StoreMsgPanier").animate({
                    opacity: 0.25
                }, 1000).animate({
                    opacity: 1
                }, 1000);
            }
        });
    });
});

DEMO - &gt;显示检索选项和选择。