javascript实现双击table的td实现编辑,并保存到数据库,需要执行以下操作:
网页上,想输入文字,是需要文本框支持的。所以,想实现完美效果,建议td中加入2个元素:
<span></span>
<input type="text">
使用jquery绑定td的双击事件dblclick,事件效果:将span的文本赋值给input的value,隐藏span,显示input
使用jquery绑定input的焦点丢失事件blur,事件效果:将input的value赋值给span的文本,隐藏input,显示span
注意,table初始化的时候,span显示,input隐藏,并且span中的文本与input的value相同
用js向cookie中保存数据、获取数据的方法如下:\x0d\x0afunction GetCookieVal(offset)\x0d\x0a//获得Cookie解码后的值\x0d\x0a{\x0d\x0avar endstr = document.cookie.indexOf ("", offset)\x0d\x0aif (endstr == -1)\x0d\x0aendstr = document.cookie.length\x0d\x0areturn unescape(document.cookie.substring(offset, endstr))\x0d\x0a}\x0d\x0a\x0d\x0a//---------------------------\x0d\x0afunction SetCookie(name, value)\x0d\x0a//设定Cookie值\x0d\x0a{\x0d\x0avar expdate = new Date()\x0d\x0avar argv = SetCookie.arguments\x0d\x0avar argc = SetCookie.arguments.length\x0d\x0avar expires = (argc >2) ? argv[2] : null\x0d\x0avar path = (argc >3) ? argv[3] : null\x0d\x0avar domain = (argc >4) ? argv[4] : null\x0d\x0avar secure = (argc >5) ? argv[5] : false\x0d\x0aif(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ))\x0d\x0adocument.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("expires="+ expdate.toGMTString()))\x0d\x0a+((path == null) ? "" : ("path=" + path)) +((domain == null) ? "" : ("domain=" + domain))\x0d\x0a+((secure == true) ? "secure" : "")\x0d\x0a}\x0d\x0a\x0d\x0a//---------------------------------\x0d\x0afunction DelCookie(name)\x0d\x0a//删除Cookie\x0d\x0a{\x0d\x0avar exp = new Date()\x0d\x0aexp.setTime (exp.getTime() - 1)\x0d\x0avar cval = GetCookie (name)\x0d\x0adocument.cookie = name + "=" + cval + "expires="+ exp.toGMTString()\x0d\x0a}\x0d\x0a\x0d\x0a//------------------------------------\x0d\x0afunction GetCookie(name)\x0d\x0a//获得Cookie的原始值\x0d\x0a{\x0d\x0avar arg = name + "="\x0d\x0avar alen = arg.length\x0d\x0avar clen = document.cookie.length\x0d\x0avar i = 0\x0d\x0awhile (i 回答于 2022-12-11使用cookie即可。
<!DOCTYPE HTML><html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="keywords" content="白菜编辑部">
<title>白菜编辑部</title>
<style type="text/css">
</style>
<script type="text/javascript">
function readCookie (name)
{
var cookieValue = ""
var search = name + "="
if (document.cookie.length > 0)
{
offset = document.cookie.indexOf (search)
if (offset != -1)
{
offset += search.length
end = document.cookie.indexOf ("", offset)
if (end == -1)
end = document.cookie.length
cookieValue = unescape (document.cookie.substring (offset, end))
}
}
return cookieValue
}
function writeCookie (name, value, hours)
{
var expire = ""
if (hours != null)
{
expire = new Date ((new Date ()).getTime () + hours * 3600000)
expire = " expires=" + expire.toGMTString ()
}
document.cookie = name + "=" + escape (value) + expire
}
writeCookie ("myCookie", "my name", 24)
alert (readCookie ("myCookie"))
</script>
</head>
<body>
</body>
</html>