是否可以记住 div 的高度?例如,在单个网站上,我展开包含一些内容的 div,然后单击内容不同但具有相同 div(相同 ID 和类)的另一个页面。不幸的是,所有内容都会刷新并失去 div 的高度。
我得到了 html 中的框架效果(但你怎么看它并不像我想的那样工作)。怎么样?将数据保存在 session 中?需要示例...
如果有人问,我正在使用 jQuery,Spring MVC
最佳答案
您可以简单地使用 HTML5 localStorage 机制。
您可以根据需要使用.height()
、.innerHeight()
或outerHeight()
。
Here's the example - jsfiddle.net/xK0nB1n
jquery
if(localStorage.divheight) //extract the old height
{
// handle the height as you want here
alert('Old height from local storage: '+ localStorage.divheight);
}
//Code just to show the mechanism of LocalStorage
$(document).ready(function(){
$('#result').click (function () {
//Remember the height
localStorage.divheight = $("div").height();
//Tell the user the freshly stored height
alert("New height: "+localStorage.divheight);
});
});
HTML
<div id="result">
Some number of lines here....
Lorem ipsum dolor sit amet, consectetur adipisicing elit
. Fuga earum id fugit veritatis! Pariatur, magni.
</div>
关于jQuery并保存页面之间的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25580753/