javascript - 在 javascript 或 jquery 中的图像之间切换

标签 javascript jquery html

我需要在“edit.png”和“ok.png”之间切换。最初网页加载包含“edit.png”图像按钮的页面。就像下面的屏幕截图 when page loads my web page looks like this

我的要求是,一旦我单击edit.png,它应该保持为edit.png图像状态。我再次单击 edit.png,它应该更改为“ok.png”。那么我该怎么做,任何人都可以帮助我。

我尝试的是

  $(function(){
$('.edit').on('click',function(){   
   $(this).toggle();  
   //show the ok button which is just next to the edit button
   $(this).next(".ok").toggle();  
});

$('.ok').on('click',function(){ 
   $(this).toggle();  
   $(this).next(".edit").toggle();     
   });
 })

$('#projectsTable').Tabledit({
	url: '#',
	deleteButton: false,
	buttons: {
	   edit: {
		   class: 'btn btn-primary secodary',
		   html: '<img src="/concrete5/application/images/animated/btn_edit.png" class="edit" /><img src="/concrete5/application/images/animated/btn_ok.png" class="ok" style="display:none" />',
		   action: 'edit'
	  }

	},
	columns: {
	   identifier: [1, 'Projects'],
	   hideIdentifier: true,
	 editable: [[1, 'Projects'], [2, 'Subprojects'],[8, 'Project Status', '{"1": "Open", "2": "Closed"}']]
	},
	onDraw: function() {
	   console.log('onDraw()');
	},
	onSuccess: function(data, textStatus, jqXHR) {
	   console.log('onSuccess(data, textStatus, jqXHR)');
	   console.log(data);
	   console.log(textStatus);
	   console.log(jqXHR);
	},
	onFail: function(jqXHR, textStatus, errorThrown) {
	   console.log('onFail(jqXHR, textStatus, errorThrown)');
	   console.log(jqXHR);
	   console.log(textStatus);
	   console.log(errorThrown);
	},
	onAlways: function() {
	   console.log('onAlways()');
	},
	onAjax: function(action, serialize) {
		console.log('onAjax(action, serialize)');
		console.log(action);
		console.log(serialize);
	}
});

$(function(){
	$('.edit').on('click',function(){   
		$(this).toggle();  
		//show the ok button which is just next to the edit button
		$(this).next(".ok").toggle();  
	});
	$('.ok').on('click',function(){ 
		$(this).toggle();  
		$(this).next(".edit").toggle();     
	});
})

最佳答案

使用点击计数器,因此当您第二次单击该按钮时,它会显示“编辑”按钮并将计数器重置回 0。

当您单击“确定”按钮时,它会变回“编辑”按钮,并且由于您现在已经完成了第一次编辑,因此下次单击该按钮时,它会立即更改为“确定”。

$('.edit').each(function() {
  var clickCounter = 0, // Sets click counter to 0 - No clicks done yet
      firstEdit = false; // Sets first edit to false
  $(this).on('click', function() {
      clickCounter++;
      if( clickCounter == 2 || firstEdit == true ) {
        $(this).toggle();
        $(this).next('.ok').toggle();
        clickCounter = 0; // Reset counter
        firstEdit = true;
      }
  });
});
$('.ok').on('click', function() {
    $(this).toggle();
  $(this).prev('.edit').toggle();
});

Working Fiddle

关于javascript - 在 javascript 或 jquery 中的图像之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41660573/

相关文章:

javascript - 不同浏览器的 document.body.offsetWidth 变化

javascript - jQuery动态添加按钮,文本不在按钮中

html - Font Awesome 图标没有出现在文本框内

javascript - 谷歌地图 : How to create a custom InfoWindow?

javascript - jQuery 是否在内部将 HTML5 数据属性键转换为小写?

javascript - 由 : com. intuit.karate.exception.KarateException: ReferenceError: "XMLHttpRequest"is not defined Karate 配置 js 文件中的异常引起

javascript - 如何使用 Vanilla JS 将按钮切换到事件状态?

javascript - 从 HTML 按钮播放 Javascript 播放器

javascript - While 循环使用 jQuery 异步 AJAX 调用

javascript - 在点击事件上创建统一的滚动速度