简体   繁体   English

使用jQuery将其清空后重新获得HTML?

[英]regain html after emptying it using jquery?

I have an if/else loop inside my jQuery. 我的jQuery内部有一个if / else循环。 I need to hide HTML on my text box inside my if portion and regain it at my else part. 我需要在我的if部分内部的文本框中隐藏HTML,然后在else部分中重新获得它。 How can I do this using jQuery? 我该如何使用jQuery?

if (test == 1) {
    $(#id).html(""); //code to hide html
} else {
    //code to regain
}

code to hide html will works fine,but how to regain the code after emptying it? 隐藏html的代码可以正常工作,但是清空后如何重新获取代码?

you can't regain lost HTML unless you have saved it somewhere, 除非您将其保存在某处,否则您将无法找回丢失的HTML,

you can either show/hide the element 您可以显示/隐藏元素

if(test==1){
   $(#id).hide();
}
else{
   $(#id).show();
}

or you can store the values somewhere in a variable 或者您可以将值存储在变量中的某个位置

var html = "";
if (test == 1) {
    html = $(#id).html(); 
    $(#id).html( "" ); 
} else {
    $(#id).html( html ); 
}

or set it to localstorage (if you browser supports it) 或将其设置为本地存储(如果您的浏览器支持)

if (test == 1) {
    localStorage.setItem("savedHTML", $(#id).html()) ; 
    $(#id).html( "" ); 
} else {
    $(#id).html( localStorage.getItem( "savedHTML" ) ); 
}

You can assign the complete html inside your div to a variable. 您可以将div中的完整html分配给变量。

var htmlContent = $(#id).html();

Then in your else, you can assign it back. 然后在其他位置,您可以将其分配回来。

$(#id).html(htmlContent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM