• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
小不点儿大的由我做主!
流年似水!
博客园    首页    新随笔    联系   管理    订阅  订阅

JQuery示例1-实现同一页面的编辑功能

html主页面:

Code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    
<title>Edit-in-Place with jQuery</title>
    
<script type="text/javascript" src="js/jquery-1.2.6.min.js"></script>
<script src="js/jqueryEIP2.js" type="text/javascript"></script>

    
</head>

<body>

<h1>Edit-in-Place with jQuery</h1>

<div id="editInPlace"> 
<p>Dashing <strong>through</strong> the snow on a 
one-horse open sleigh, over the fields we go, laughing all the way; bells on 
bob-tail ring, making spirits bright, what fun it is to ride and sing a sleighing 
song tonight. Jingle bells, jingle bells, Jingle all the way! O what fun it 
is to ride in a one-horse open sleigh. Oh! Jingle bells, jingle bells, Jingle 
all the way! O what fun it is to ride in a one-horse open sleigh.
</p>
<p>Let's see what another p does</p>
<p><strong>Note:</strong> This example does not send the results to a server side script for active saving of changed content. It would not be difficult to use jQuery to send the saved content to a PHP or perl script (or Rails) using AHAH.</p>

</div>

</body>
</html

 

jqueryEIP2.js文件

Code
$(document).ready(function(){
   setEdit();
});


function setEdit()
{
$(
".edit").click(function(){
   $(
this).remove();
   
var textarea = '<div><textarea rows="10" cols="60">' + $("#dialog").html() + '</textarea>';
   
var button = '<div><input type="button" value="保存" class="save" /> <input type="button" value="取消" class="cancel"/></div></div>';
   
var revert = $("#dialog").html();
   $(
"#dialog").after(textarea+button).remove();
   $(
".save").click(function(){saveChanges(this,false);});
   $(
".cancel").click(function(){saveChanges(this,revert);});
}); 
};

function saveChanges(obj,cancel)
{
if (!cancel)
{
   
var t = $(obj).parent().siblings(0).val();
}
else {
var t = cancel;
}
if(t=='') t='(click to add text)';
var bt='<input type="button" class="edit" value="编辑" />';
$(obj).parent().parent().after(
'<div id="dialog">'+t+'</div><br />'+bt).remove();
setEdit();

 

posted @ 2008-10-06 10:49  小不点儿大的由我做主!  阅读(278)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3