Javascript Jquery change.text(某物)然后返回
如果我有以下代码: htmlJavascript Jquery change.text(某物)然后返回,javascript,jquery,Javascript,Jquery,如果我有以下代码: html jquery将删除图像并将其替换为“Correct”。这只是在我需要图像重新出现之前的延迟。因此,它将正确闪烁,然后返回原始图像。您需要存储旧内容,然后: var oldhtml = $('.lg').html(); // WARNING: will give unexpected results if .lg matches more than one element $('.lg').text("Correct"); setTimeout(function()
jquery将删除图像并将其替换为“Correct”。这只是在我需要图像重新出现之前的延迟。因此,它将正确闪烁,然后返回原始图像。您需要存储旧内容,然后:
var oldhtml = $('.lg').html();
// WARNING: will give unexpected results if .lg matches more than one element
$('.lg').text("Correct");
setTimeout(function(){
$('.lg').html(oldhtml);
}, 1000); // milliseconds
如果有多个项具有classlg
,则需要一个循环:
$('.lg').each(function() {
var oldhtml = $(this).html();
$(this).text("Correct");
setTimeout(function(){
$(this).html(oldhtml);
}, 1000); // milliseconds
});
您需要存储旧内容,然后:
var oldhtml = $('.lg').html();
// WARNING: will give unexpected results if .lg matches more than one element
$('.lg').text("Correct");
setTimeout(function(){
$('.lg').html(oldhtml);
}, 1000); // milliseconds
如果有多个项具有classlg
,则需要一个循环:
$('.lg').each(function() {
var oldhtml = $(this).html();
$(this).text("Correct");
setTimeout(function(){
$(this).html(oldhtml);
}, 1000); // milliseconds
});
您需要将图像(原始文本)保存为一个字符串,对其进行更改,然后再进行更改
var orig = $('.lg').html(); // This should be .html, not .text
// so that it saves the <img/> tag
$('.lg').text('Correct');
// After a delay
setTimeout(function(){
$('.lg').html(orig); // Use .html here, so that the <img/> tag will work
}, 1000);
您需要将图像(原始文本)保存为一个字符串,对其进行更改,然后再进行更改
var orig = $('.lg').html(); // This should be .html, not .text
// so that it saves the <img/> tag
$('.lg').text('Correct');
// After a delay
setTimeout(function(){
$('.lg').html(orig); // Use .html here, so that the <img/> tag will work
}, 1000);
你可以试试这样的
$('.lg').find('img,span').toggle(); // This will hide the image and show the text
// run it again to change it back
$('.lg').find('img').fadeIn();
$('.lg').find("span").fadeOut();
显示图像
$('.lg').find('img').fadeOut();
$('.lg').find("span").fadeIn();
最后,您的代码可以是这样的
$('.lg').find('img,span').toggle(); // This will hide the image and show the text
// run it again to change it back
$('.lg').find('img').fadeIn();
$('.lg').find("span").fadeOut();
你可以试试这样的
$('.lg').find('img,span').toggle(); // This will hide the image and show the text
// run it again to change it back
$('.lg').find('img').fadeIn();
$('.lg').find("span").fadeOut();
显示图像
$('.lg').find('img').fadeOut();
$('.lg').find("span").fadeIn();
最后,您的代码可以是这样的
$('.lg').find('img,span').toggle(); // This will hide the image and show the text
// run it again to change it back
$('.lg').find('img').fadeIn();
$('.lg').find("span").fadeOut();
因此,您必须将另一个更改设置为超时。因为更改实际上会替换dom中的对象。所以您必须在超时的情况下进行另一个更改。因为更改实际上会替换dom中的对象。在我看来,实现这一点的更简洁的方法是对图像和文本使用不同的类,并分别隐藏/显示它们: CSS: HTML:
我认为,实现这一点的一种更简洁的方法是对图像和文本使用不同的类,并分别隐藏/显示它们: CSS: HTML:
与翻转div的html不同,我建议让“正确”标签和图像都存在于div中,然后打开和关闭这两个选项:
$(".lg .image").hide();
$(".lg .text").show();
setTimeout(function() {
$(".lg .image").show();
$(".lg .text").hide();
}, 1000);
对的
$(文档).ready(函数(){
//从一开始就隐藏正确的跨度
$('#correctSpan').toggle();
});
//在切换两个事件的事件处理程序中。。。
$('#correctSpan,#image')。toggle();
我建议将“正确”标签和图像都放在div中,然后打开和关闭它们,而不是翻转div的html:
$(".lg .image").hide();
$(".lg .text").show();
setTimeout(function() {
$(".lg .image").show();
$(".lg .text").hide();
}, 1000);
对的
$(文档).ready(函数(){
//从一开始就隐藏正确的跨度
$('#correctSpan').toggle();
});
//在切换两个事件的事件处理程序中。。。
$('#correctSpan,#image')。toggle();
像这样的东西怎么样:
CSS:
HTML:
像这样的怎么样: CSS: HTML:
我不太明白你的问题。你是否需要图像消失一秒钟,然后显示正确的信息,然后图像重新出现?你可以尝试下面描述的方法:Alex,是的,非常正确..我不太理解你的问题。您是否需要图像消失一秒钟以显示正确的消息,然后图像重新出现?您可以尝试此处描述的方法:Alex,是的,这完全正确..我不认为.text()会做您认为它会做的事情。。。我想应该是.html()。@jbabey:你说得对,我需要
.html
这样
标记才能工作。修正了。我不认为。text()会做你认为它会做的事情。。。我想应该是.html()。@jbabey:你说得对,我需要.html
这样
标记才能工作。已修复。很抱歉,我根本不清楚您在这里想说什么。我的意思是,为了做您想做的事情,您必须在执行更改()之前备份变量中的当前标记内容,然后设置超时,您可以使用另一个更改()还原以前的内容。基本上是@mblase75在第二段代码中所说的。很抱歉,但我一点也不清楚您在这里想说什么。我的意思是,为了做您想做的事情,您必须在执行更改()之前备份var中的当前标记内容,然后设置一个超时,您可以使用另一个更改()还原以前的内容。基本上是@mblase75在第二段代码中所说的。