简体   繁体   English

jQuery的图像选择器无法在IE7

[英]jquery image selector not working in IE7

So I have some html like so: 所以我有一些类似这样的html:

<div id="avatar_choices">
    <label for="id_choice_0">
        <input id="id_choice_0" type="radio" name="choice" value="7" />
        <img src="someimage.jpg" />
    </label>
    <label for="id_choice_1">
        <input id="id_choice_1" type="radio" name="choice" value="8" />
        <img src="someimage2.jpg" />
    </label>
</div>

And some script: 和一些脚本:

$('#avatar_choices input').hide();
$('#avatar_choices img').click(function(){
    $('#avatar_choices img').css('border', '2px solid #efefef');
    $(this).css('border', '2px solid #39c');
    $(this).siblings('input').attr('checked', 'checked');
});

The goal is to allow the user to click around on the image options, having the selected one highlight with a border colour. 目的是允许用户单击图像选项,使选中的一个高光具有边框颜色。

This works fine in FF. 这在FF中效果很好。 For some reason in IE once I click on an image, click another image, then try to click the first one, the border won't change (though it does get selected). 出于某种原因,在IE中,一旦我单击图像,单击另一个图像,然后尝试单击第一个图像,边框就不会改变(尽管它确实被选中了)。

EDIT: My solution ended up happening half by accident. 编辑:我的解决方案最终偶然发生了一半。 I changed the code to this due to redsquare's answer: 由于redsquare的回答,我将代码更改为此:

$('#avatar_choices input').hide();
$('#avatar_choices img').click(function(){
    $('#avatar_choices img').removeClass('selected');
    $(this).addClass('selected');
    $(this).siblings('input').attr('checked', 'checked');
});

where: 哪里:

#avatar_choices img.selected{border:2px solid #39c;}

Go figure. 去搞清楚。

best using addClass and removeClass in this scenario. 在这种情况下,最好使用addClass和removeClass。 Easier to maintain. 易于维护。 Can you paste your full html so I can see your doctype etc 您可以粘贴完整的html吗,以便我可以看到您的doctype等

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

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