localstorage使用(jquery怎么使用localstorage)

本文目录
- jquery怎么使用localstorage
- localstorage是什么,它有哪些作用
- android 中 webview 怎么用 localStorage
- java 怎么使用localstorage
- 如何在 ie6 中使用 “localStorage
- 请教javascript中localstorage的使用问题
jquery怎么使用localstorage
在HTML 5中,localstorage是个不错的东西,在支持localstorage的浏览器中,
能持久化用户表单的输入,即使关掉浏览器,下次重新打开浏览器访问,也能读出其值, 下面给出的例子是使用jquery
在每次表单加载的时候,读localstorage的值,而在表单每次提交时则清楚其值的例子
首先是一个表单:
代码如下:
《!DOCTYPE html》
《html lang=“en“》
《head》
《meta charset=“utf-8“》
《title》HTML5 Local Storage Example《/title》
《!-- include Bootstrap CSS for layout --》
《link href=“//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/css/bootstrap-combined.min.css“ rel=“stylesheet“》
《/head》
《body》
《div class=“container“》
《h1》HTML5 Local Storage Example《/h1》
《form method=“post“ class=“form-horizontal“》
《fieldset》
《legend》Enquiry Form《/legend》
《div class=“control-group“》
《label class=“control-label“ for=“type“》Type of enquiry《/label》
《div class=“controls“》
《select name=“type“ id=“type“》
《option value=““》Please select《/option》
《option value=“general“》General《/option》
《option value=“sales“》Sales《/option》
《option value=“support“》Support《/option》
《/select》
《/div》
《/div》
《div class=“control-group“》
《label class=“control-label“ for=“name“》Name《/label》
《div class=“controls“》
《input class=“input-xlarge“ type=“text“ name=“name“ id=“name“ value=““ maxlength=“50“》
《/div》
《/div》
《div class=“control-group“》
《label class=“control-label“ for=“email“》Email Address《/label》
《div class=“controls“》
《input class=“input-xlarge“ type=“text“ name=“email“ id=“email“ value=““ maxlength=“150“》
《/div》
《/div》
《div class=“control-group“》
《label class=“control-label“ for=“message“》Message《/label》
《div class=“controls“》
《textarea class=“input-xlarge“ name=“message“ id=“message“》《/textarea》
《/div》
《/div》
《div class=“control-group“》
《div class=“controls“》
《label class=“checkbox“》
《input name=“subscribe“ id=“subscribe“ type=“checkbox“》
Subscribe to our newsletter
《/label》
《/div》
《/div》
《/fieldset》
《div class=“form-actions“》
《input type=“submit“ name=“submit“ id=“submit“ value=“Send“ class=“btn btn-primary“》
《/div》
《/form》
《/div》
然后是js部分代码:
代码如下:
《script src=“//code.jquery.com/jquery-latest.js“》《/script》
《script》
$(document).ready(function () {
/*
* 判断是否支持localstorage
*/
if (localStorage) {
/*
* 读出localstorage中的值
*/
if (localStorage.type) {
$(“#type“).find(“option[value=“ + localStorage.type + “]“).attr(“selected“, true);
}
if (localStorage.name) {
$(“#name“).val(localStorage.name);
}
if (localStorage.email) {
$(“#email“).val(localStorage.email);
}
if (localStorage.message) {
$(“#message“).val(localStorage.message);
}
if (localStorage.subscribe === “checked“) {
$(“#subscribe“).attr(“checked“, “checked“);
}
/*
* 当表单中的值改变时,localstorage的值也改变
*/
$(“input[type=text],select,textarea“).change(function(){
$this = $(this);
localStorage[$this.attr(“name“)] = $this.val();
});
$(“input[type=checkbox]“).change(function(){
$this = $(this);
localStorage[$this.attr(“name“)] = $this.attr(“checked“);
});
$(“form“)
/*
* 如果表单提交,则调用clear方法
*/
.submit(function(){
localStorage.clear();
})
.change(function(){
console.log(localStorage);
});
}
});
localstorage是什么,它有哪些作用
局部存储器。它是html5新增的一个本地存储API,所谓localStorage就是一个小仓库的意思,它有5M的大小空间,存储在浏览器中,我们可以通过js来操纵localStorage。
localStorage的用法
1、localStorage通过使用setItem(key,value)来设置元素以及值,如localStorage.setItem(“name“,“chenyejun“)
2、使用getItem(key)来获取元素值,如localStorage.getItem(“name“);
3、使用removeItem(key)清除key值,如localStorage.removeItem(“name“);
4、使用localStorage.clear()清除所有的key值;
5、使用localStorage.length可以获取本地存储key的个数;
6、按照序号读取本地存储变量的key值,使用localStorage.key(i)。
android 中 webview 怎么用 localStorage
localStorage是javascript中的本地存储器,android中只需要使用webView去加载html即可。
WebView(网络视图)能加载显示网页,可以将其视为一个浏览器。它使用了WebKit渲染引擎加载显示网页,实现WebView有以下两种不同的方法:
第一种方法的步骤:
1.在要Activity中实例化WebView组件:WebView webView = new WebView(this);
2.调用WebView的loadUrl()方法,设置WevView要显示的网页:
互联网用:webView.loadUrl(“
《uses-permission android:name=“android.permission.INTERNET“/》
java 怎么使用localstorage
在HTML 5中,localstorage是个不错的东西,在支持localstorage的浏览器中, 能持久化用户表单的输入,即使关掉浏览器,下次重新打开浏览器访问,也能读出其值, 下面给出的例子是使用jquery 在每次表单加载的时候,读localstorage的值,而在表单每次提交时则清楚其值的例子
首先是一个表单:
复制代码 代码如下:
《!DOCTYPE html》
《html lang=“en“》
《head》
《meta charset=“utf-8“》
《title》HTML5 Local Storage Example《/title》
《!-- include Bootstrap CSS for layout --》
《link rel=“stylesheet“》
《/head》
《body》
《div class=“container“》
《h1》HTML5 Local Storage Example《/h1》
《form method=“post“ class=“form-horizontal“》
《fieldset》
《legend》Enquiry Form《/legend》
《div class=“control-group“》
《label class=“control-label“ for=“type“》Type of enquiry《/label》
《div class=“controls“》
《select name=“type“ id=“type“》
《option value=““》Please select《/option》
《option value=“general“》General《/option》
《option value=“sales“》Sales《/option》
《option value=“support“》Support《/option》
《/select》
《/div》
《/div》
《div class=“control-group“》
《label class=“control-label“ for=“name“》Name《/label》
《div class=“controls“》
《input class=“input-xlarge“ type=“text“ name=“name“ id=“name“ value=““ maxlength=“50“》
《/div》
《/div》
《div class=“control-group“》
《label class=“control-label“ for=“email“》Email Address《/label》
《div class=“controls“》
《input class=“input-xlarge“ type=“text“ name=“email“ id=“email“ value=““ maxlength=“150“》
《/div》
《/div》
《div class=“control-group“》
《label class=“control-label“ for=“message“》Message《/label》
《div class=“controls“》
《textarea class=“input-xlarge“ name=“message“ id=“message“》《/textarea》
《/div》
《/div》
《div class=“control-group“》
《div class=“controls“》
《label class=“checkbox“》
《input name=“subscribe“ id=“subscribe“ type=“checkbox“》
Subscribe to our newsletter
《/label》
《/div》
《/div》
《/fieldset》
《div class=“form-actions“》
《input type=“submit“ name=“submit“ id=“submit“ value=“Send“ class=“btn btn-primary“》
《/div》
《/form》
《/div》
然后是js部分代码:
复制代码 代码如下:
《script src=“///jquery-latest.js“》《/script》
《script》
$(document).ready(function () {
/*
* 判断是否支持localstorage
*/
if (localStorage) {
/*
* 读出localstorage中的值
*/
if (localStorage.type) {
$(“#type“).find(“option[value=“ + localStorage.type + “]“).attr(“selected“, true);
}
if (localStorage.name) {
$(“#name“).val(localStorage.name);
}
if (localStorage.email) {
$(“#email“).val(localStorage.email);
}
if (ssage) {
$(“#message“).val(ssage);
}
if (localStorage.subscribe === “checked“) {
$(“#subscribe“).attr(“checked“, “checked“);
}
/*
* 当表单中的值改变时,localstorage的值也改变
*/
$(“input[type=text],select,textarea“).change(function(){
$this = $(this);
localStorage[$this.attr(“name“)] = $this.val();
});
$(“input[type=checkbox]“).change(function(){
$this = $(this);
localStorage[$this.attr(“name“)] = $this.attr(“checked“);
});
$(“form“)
/*
* 如果表单提交,则调用clear方法
*/
.submit(function(){
localStorage.clear();
})
.change(function(){
console.log(localStorage);
});
}
});
如何在 ie6 中使用 “localStorage
方法如下:
1、先下载Companion.JS安装文件,然后安装。
2、下载安装Microsoft Script Debugger,如果您的机器已经安装过了就可以免过这一步。
3、打开IE菜单“工具”--“Internet选项”--“高级”,找到“禁用脚本调试(Internet Explorer)”和“禁用脚本调试(在Internet Explorer之外)”,将两个选项前面的对钩都去掉,然后重启IE。
请教javascript中localstorage的使用问题
localstorage 属于html5的产品,有的浏览器可能不认识,
使用方法
if(window.localStorage){
localStorage[’value2’] = document.getElementById(’show2’).value;
}
推荐使用cookie

本文相关文章:
soldierstory(求推荐些soldierstory的兵人啊)
2026年10月8日 17:40
cocos creator官网(Cocos Creator 和 Cocos2d-x Js 之间到底是啥关系)
2026年7月23日 20:30
localstorage生命周期(localStorage与sessionStorage到底存储的是什么,具体)
2026年7月5日 13:20
更多文章:
another time(another time和other time的区别)
2026年10月11日 05:00







