我想用id =“slider_arrow”更改我的span内的img的src属性,
<span id="slider_arrow">
<img />
</span>
每次我的jquery函数运行时:
$("#0").click(function () {
/*Rest of the function*/
var arrow = document.getElementById('slider_arrow');
/*I want to add different slider_arrow's sprite each time i call this function*/
});
CSS:
.red_arrow_sprite{
width:25px;
height:12px;
background:url("/Images/arrows.png") 0 0px;
}
.yellow_arrow_sprite{
width:25px;
height:12px;
background:url("/Images/arrows.png") -26px 0px;
}
.black_arrow_sprite{
width:25px;
height:12px;
background:url("/Images/arrows.png") -51px 0px;
}
我的CSS Sprite文件宽度为75px,高度为12px。每张图片都有25px / 12px。
问题是:
1)我是否正确编写了CSS规则?
2)我需要用什么来改变跨度内的img的src?
谢谢!
答案 0 :(得分:1)
$("#0").click(function () {
var classes = ['red_arrow_sprite','yellow_arrow_sprite','black_arrow_sprite'];
var $span = $('#slider_arrow');
$span.attr('class', (classes[($.inArray($span.attr('class'), classes)+1)%classes.length]));
});
答案 1 :(得分:0)
删除span
中的图像并使用jQuery将css类设置为Slider,试试此代码
<span id="slider_arrow" class="red_arrow_sprite">
</span>
$("#0").click(function () {
var item = $('#slider_arrow');
if (item.hasClass('red_arrow_sprite')){
item.removeClass('red_arrow_sprite').addClass('yellow_arrow_sprite');
} else if (item.hasClass('yellow_arrow_sprite')){
item.removeClass('yellow_arrow_sprite').addClass('black_arrow_sprite');
} else {
item.removeClass('black_arrow_sprite').addClass('red_arrow_sprite');
}
});