javascript - 带有图像的单选按钮

标签 javascript jquery html css

如何在单击单选按钮时更改图像? 我有点击更改 img 的笑脸。(静态 img- happy.png,事件 img- happy_1.png)

fiddle :http://jsfiddle.net/ejff1j9x/4/

HTML:

<div id="sites">
    <input type="radio" name="site" id="so" value="stackoverflow" /><label for="so"><img src="http://impalz.co/ratingicons/Sad.png" alt="Stack Overflow" /></label>
    <input type="radio" name="site" id="sf" value="serverfault" /><label for="sf"><img src="http://impalz.co/ratingicons/okay.png" alt="Server Fault" /></label>
    <input type="radio" name="site" id="su" value="superuser" /><label for="su"><img class="imgs" src="http://impalz.co/ratingicons/happy.png" alt="Super User" /></label>
</div>

JS:

$('#sites input:radio').addClass('input_hidden');

$('#sites label').click(function() {
        $(this).addClass('selected').siblings().removeClass('selected');
});

CSS:

.input_hidden {
    position: absolute;
    left: -9999px;
}
.selected {
    content: url(http://impalz.co/ratingicons/happy_1.png);
}

我可以用所有其他图像更改一张静态图像。

最佳答案

我没有正确地回答您的问题,这就是您正在尝试的问题,请检查以下代码:

$('#sites label').click(function() {
    $(this).find("img").attr("src","imagepath");
    $(this).addClass('selected').siblings().removeClass('selected');
});

关于javascript - 带有图像的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31717420/

相关文章:

javascript - iframe 在 chrome/safari 中使用 scrollheight 调整大小

javascript - 按特定单词 react 排序

html - 相对元素下的绝对元素导致 Firefox 下的奇怪行为

javascript - 单击按钮时如何滚动到元素并使其悬停?

javascript - 如何从firebase云功能连接到MongoDb Atlas

javascript - 使用 jQuery 到 'click' 一个 li 元素

javascript - mCustomScrollbar 不适用于 jquery 附加

javascript - 尝试避免在 Domino XPage 中多次调用 jQuery

javascript - 修改jQuery函数(抓取每个DOM元素)

javascript - 使用 Inquire 进行布局而不是使用 CSS 进行布局