0
我正在尋找重寫下面的代碼,以便在第一次單擊時,而不是隻更改選擇器元素的圖像src,第二次單擊將圖像src更改爲images/spacer.gif,隨後的任何點擊都會在添加圖像src和將其替換爲間隔符之間循環。因爲這個函數使用單選按鈕和一個(事件),所以如果可能的話,我想遠離.toggle。下面的代碼塊僅適用於第一個功能。切換沒有使用.toggle
$('.item-button').click(function(event) {
var layerID = $(this).attr('data-layer');
var itemID = $(this).attr('data-item');
var selector = '#layer-' + layerID;
//an itemID of 0 means the item doesn't exist.
if (itemID == 0) {
$('#layer-' + layerID).attr('src', 'images/spacer.gif');
} else {
var imageSrc = $(this).parent().find('img').attr('id');
$(selector).attr('src', imageSrc);
}
},
//a second nonfunctional function to change the src to the spacer in case the same radio button is clicked twice.
function(){
$('#layer-' + layerID).attr('src', 'images/spacer.gif');
});