Jquery 单击即可更改图像,单击任意位置即可恢复图像
我正在尝试创建一个脚本: 单击图像后更改图像,Jquery 单击即可更改图像,单击任意位置即可恢复图像,jquery,Jquery,我正在尝试创建一个脚本: 单击图像后更改图像, 然后恢复到原始图像, 无论何时单击屏幕上的任何位置 当前,我的脚本仅在单击确切的图像时更改 单击时更改的图像: <!-- CardStatus --> <img src="toggle-on.png" class="img-swap"/> 下面是一个更新的代码,示例使用了Joel建议的.bind(),因为他是对的,它对这一点有最好的支持。如果单击图像,它将交换图像,如果再次单击,它将更改回,以及单击身体上的任何位置。由于图
然后恢复到原始图像,
无论何时单击屏幕上的任何位置 当前,我的脚本仅在单击确切的图像时更改 单击时更改的图像:
<!-- CardStatus -->
<img src="toggle-on.png" class="img-swap"/>
下面是一个更新的代码,示例使用了Joel建议的
.bind()
,因为他是对的,它对这一点有最好的支持。如果单击图像,它将交换图像,如果再次单击,它将更改回,以及单击身体上的任何位置。由于图像主机的动态特性,我不得不使用完整的URL,但是您最初的替换代码应该可以正常工作
我们没有运行两个函数,而是将其缩减为一个。这可能不适用于其他。单击()
var img = $('.img-swap'),
imgs = new Array('http://s27.postimg.org/i6knc8y0j/img1.jpg', 'http://s12.postimg.org/k68so1del/img2.jpg'); // Off is the first image
isOn = false;
$(document).bind("click", function(e) {
if ( $(e.target).attr('class') == img.attr('class') ) { // Did we click on the image?
if ( isOn == false ) {
img.attr('src', imgs[1]);
isOn = true;
} else {
img.attr('src', imgs[0]);
isOn = false;
}
} else { // Nope, must be the document or something else
if ( isOn ) {
img.attr('src', imgs[0]);
isOn = false;
}
}
});
下面的方法尝试使用动态元素。如果同时翻转多张卡,并且只关闭最后单击的卡,则此操作将不起作用
var lastClick = undefined;
$(document).bind("click", function(e) {
if ( $(e.target).attr('class') == 'img-swap' ) {
var targetimg = $(e.target);
lastClick = $(e.target);
if ( targetimg.attr('src').indexOf('toggle-on') !== -1 ) {
targetimg.attr('src', targetimg.attr('src').replace('toggle-on', 'toggle-off'));
} else if ( targetimg.attr('src').indexOf('toggle-off') !== -1 ) {
targetimg.attr('src', targetimg.attr('src').replace('toggle-off', 'toggle-on'));
}
} else {
if ( lastClick && lastClick.attr('src').indexOf('toggle-on') !== -1 ) {
lastClick.attr('src', lastClick.attr('src').replace('toggle-on', 'toggle-off'));
lastClick = undefined;
}
}
});
因此,如果像本例中那样翻转多张卡,您可能需要使用类img swap
一次关闭所有图像标记
var imageTags = $('.img-swap'),
lastClick = undefined;
$(document).bind("click", function(e) {
if ( $(e.target).attr('class') == 'img-swap' ) {
var targetimg = $(e.target);
lastClick = $(e.target);
if ( targetimg.attr('src').indexOf('toggle-on') !== -1 ) {
targetimg.attr('src', targetimg.attr('src').replace('toggle-on', 'toggle-off'));
} else if ( targetimg.attr('src').indexOf('toggle-off') !== -1 ) {
targetimg.attr('src', targetimg.attr('src').replace('toggle-off', 'toggle-on'));
}
} else {
imageTags.each(function() {
if ( $(this).attr('src').indexOf('toggle-on') !== -1 ) {
$(this).attr('src', $(this).attr('src').replace('toggle-on', 'toggle-off'));
}
});
}
});
下面是一个更新的代码,示例使用了Joel建议的.bind()
,因为他是对的,它对这一点有最好的支持。如果单击图像,它将交换图像,如果再次单击,它将更改回,以及单击身体上的任何位置。由于图像主机的动态特性,我不得不使用完整的URL,但是您最初的替换代码应该可以正常工作
我们没有运行两个函数,而是将其缩减为一个。这可能不适用于其他。单击()
var img = $('.img-swap'),
imgs = new Array('http://s27.postimg.org/i6knc8y0j/img1.jpg', 'http://s12.postimg.org/k68so1del/img2.jpg'); // Off is the first image
isOn = false;
$(document).bind("click", function(e) {
if ( $(e.target).attr('class') == img.attr('class') ) { // Did we click on the image?
if ( isOn == false ) {
img.attr('src', imgs[1]);
isOn = true;
} else {
img.attr('src', imgs[0]);
isOn = false;
}
} else { // Nope, must be the document or something else
if ( isOn ) {
img.attr('src', imgs[0]);
isOn = false;
}
}
});
下面的方法尝试使用动态元素。如果同时翻转多张卡,并且只关闭最后单击的卡,则此操作将不起作用
var lastClick = undefined;
$(document).bind("click", function(e) {
if ( $(e.target).attr('class') == 'img-swap' ) {
var targetimg = $(e.target);
lastClick = $(e.target);
if ( targetimg.attr('src').indexOf('toggle-on') !== -1 ) {
targetimg.attr('src', targetimg.attr('src').replace('toggle-on', 'toggle-off'));
} else if ( targetimg.attr('src').indexOf('toggle-off') !== -1 ) {
targetimg.attr('src', targetimg.attr('src').replace('toggle-off', 'toggle-on'));
}
} else {
if ( lastClick && lastClick.attr('src').indexOf('toggle-on') !== -1 ) {
lastClick.attr('src', lastClick.attr('src').replace('toggle-on', 'toggle-off'));
lastClick = undefined;
}
}
});
因此,如果像本例中那样翻转多张卡,您可能需要使用类img swap
一次关闭所有图像标记
var imageTags = $('.img-swap'),
lastClick = undefined;
$(document).bind("click", function(e) {
if ( $(e.target).attr('class') == 'img-swap' ) {
var targetimg = $(e.target);
lastClick = $(e.target);
if ( targetimg.attr('src').indexOf('toggle-on') !== -1 ) {
targetimg.attr('src', targetimg.attr('src').replace('toggle-on', 'toggle-off'));
} else if ( targetimg.attr('src').indexOf('toggle-off') !== -1 ) {
targetimg.attr('src', targetimg.attr('src').replace('toggle-off', 'toggle-on'));
}
} else {
imageTags.each(function() {
if ( $(this).attr('src').indexOf('toggle-on') !== -1 ) {
$(this).attr('src', $(this).attr('src').replace('toggle-on', 'toggle-off'));
}
});
}
});
下面是一个更新的代码,示例使用了Joel建议的.bind()
,因为他是对的,它对这一点有最好的支持。如果单击图像,它将交换图像,如果再次单击,它将更改回,以及单击身体上的任何位置。由于图像主机的动态特性,我不得不使用完整的URL,但是您最初的替换代码应该可以正常工作
我们没有运行两个函数,而是将其缩减为一个。这可能不适用于其他。单击()
var img = $('.img-swap'),
imgs = new Array('http://s27.postimg.org/i6knc8y0j/img1.jpg', 'http://s12.postimg.org/k68so1del/img2.jpg'); // Off is the first image
isOn = false;
$(document).bind("click", function(e) {
if ( $(e.target).attr('class') == img.attr('class') ) { // Did we click on the image?
if ( isOn == false ) {
img.attr('src', imgs[1]);
isOn = true;
} else {
img.attr('src', imgs[0]);
isOn = false;
}
} else { // Nope, must be the document or something else
if ( isOn ) {
img.attr('src', imgs[0]);
isOn = false;
}
}
});
下面的方法尝试使用动态元素。如果同时翻转多张卡,并且只关闭最后单击的卡,则此操作将不起作用
var lastClick = undefined;
$(document).bind("click", function(e) {
if ( $(e.target).attr('class') == 'img-swap' ) {
var targetimg = $(e.target);
lastClick = $(e.target);
if ( targetimg.attr('src').indexOf('toggle-on') !== -1 ) {
targetimg.attr('src', targetimg.attr('src').replace('toggle-on', 'toggle-off'));
} else if ( targetimg.attr('src').indexOf('toggle-off') !== -1 ) {
targetimg.attr('src', targetimg.attr('src').replace('toggle-off', 'toggle-on'));
}
} else {
if ( lastClick && lastClick.attr('src').indexOf('toggle-on') !== -1 ) {
lastClick.attr('src', lastClick.attr('src').replace('toggle-on', 'toggle-off'));
lastClick = undefined;
}
}
});
因此,如果像本例中那样翻转多张卡,您可能需要使用类img swap
一次关闭所有图像标记
var imageTags = $('.img-swap'),
lastClick = undefined;
$(document).bind("click", function(e) {
if ( $(e.target).attr('class') == 'img-swap' ) {
var targetimg = $(e.target);
lastClick = $(e.target);
if ( targetimg.attr('src').indexOf('toggle-on') !== -1 ) {
targetimg.attr('src', targetimg.attr('src').replace('toggle-on', 'toggle-off'));
} else if ( targetimg.attr('src').indexOf('toggle-off') !== -1 ) {
targetimg.attr('src', targetimg.attr('src').replace('toggle-off', 'toggle-on'));
}
} else {
imageTags.each(function() {
if ( $(this).attr('src').indexOf('toggle-on') !== -1 ) {
$(this).attr('src', $(this).attr('src').replace('toggle-on', 'toggle-off'));
}
});
}
});
下面是一个更新的代码,示例使用了Joel建议的.bind()
,因为他是对的,它对这一点有最好的支持。如果单击图像,它将交换图像,如果再次单击,它将更改回,以及单击身体上的任何位置。由于图像主机的动态特性,我不得不使用完整的URL,但是您最初的替换代码应该可以正常工作
我们没有运行两个函数,而是将其缩减为一个。这可能不适用于其他。单击()
var img = $('.img-swap'),
imgs = new Array('http://s27.postimg.org/i6knc8y0j/img1.jpg', 'http://s12.postimg.org/k68so1del/img2.jpg'); // Off is the first image
isOn = false;
$(document).bind("click", function(e) {
if ( $(e.target).attr('class') == img.attr('class') ) { // Did we click on the image?
if ( isOn == false ) {
img.attr('src', imgs[1]);
isOn = true;
} else {
img.attr('src', imgs[0]);
isOn = false;
}
} else { // Nope, must be the document or something else
if ( isOn ) {
img.attr('src', imgs[0]);
isOn = false;
}
}
});
下面的方法尝试使用动态元素。如果同时翻转多张卡,并且只关闭最后单击的卡,则此操作将不起作用
var lastClick = undefined;
$(document).bind("click", function(e) {
if ( $(e.target).attr('class') == 'img-swap' ) {
var targetimg = $(e.target);
lastClick = $(e.target);
if ( targetimg.attr('src').indexOf('toggle-on') !== -1 ) {
targetimg.attr('src', targetimg.attr('src').replace('toggle-on', 'toggle-off'));
} else if ( targetimg.attr('src').indexOf('toggle-off') !== -1 ) {
targetimg.attr('src', targetimg.attr('src').replace('toggle-off', 'toggle-on'));
}
} else {
if ( lastClick && lastClick.attr('src').indexOf('toggle-on') !== -1 ) {
lastClick.attr('src', lastClick.attr('src').replace('toggle-on', 'toggle-off'));
lastClick = undefined;
}
}
});
因此,如果像本例中那样翻转多张卡,您可能需要使用类img swap
一次关闭所有图像标记
var imageTags = $('.img-swap'),
lastClick = undefined;
$(document).bind("click", function(e) {
if ( $(e.target).attr('class') == 'img-swap' ) {
var targetimg = $(e.target);
lastClick = $(e.target);
if ( targetimg.attr('src').indexOf('toggle-on') !== -1 ) {
targetimg.attr('src', targetimg.attr('src').replace('toggle-on', 'toggle-off'));
} else if ( targetimg.attr('src').indexOf('toggle-off') !== -1 ) {
targetimg.attr('src', targetimg.attr('src').replace('toggle-off', 'toggle-on'));
}
} else {
imageTags.each(function() {
if ( $(this).attr('src').indexOf('toggle-on') !== -1 ) {
$(this).attr('src', $(this).attr('src').replace('toggle-on', 'toggle-off'));
}
});
}
});
由于@WASasquatch,将“正文”更改为文档。可以找到这样做的理由
您可以将重置回调附加到正文。onclick
事件:
$(".img-swap").click(function(e) {
e.stopPropagation() // Stop this click from triggering the <body> click event
/* toggle code here */
});
$(document).click(function(e) {
/* un-toggle code here */
});
$(“.img交换”)。单击(函数(e){
e、 stopPropagation()//停止此单击以触发单击事件
/*切换代码在这里*/
});
$(文档)。单击(函数(e){
/*在这里取消切换代码*/
});
另一方面,live()。您使用它而不是bind()
*或on()
***有什么特殊原因吗
*bind()
自jQuery 1.0以来一直存在,因此总体上是目前为止最好的支持
**on()
是最新的事件附加程序,将来可能会弃用bind()
bind()
映射到源代码中的on()
。由于@WASasquatch,将的“body”
更改为文档。可以找到这样做的理由
您可以将重置回调附加到正文。onclick
事件:
$(".img-swap").click(function(e) {
e.stopPropagation() // Stop this click from triggering the <body> click event
/* toggle code here */
});
$(document).click(function(e) {
/* un-toggle code here */
});
$(“.img交换”)。单击(函数(e){
e、 stopPropagation()//停止此单击以触发单击事件
/*切换代码在这里*/
});
$(文档)。单击(函数(e){
/*在这里取消切换代码*/
});
另一方面,live()。您使用它而不是bind()
*或on()
***有什么特殊原因吗
*bind()
自jQuery 1.0以来一直存在,因此总体上是目前为止最好的支持
**on()
是最新版本