委托应用-表单的创建和编辑

Posted tis100204

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了委托应用-表单的创建和编辑相关的知识,希望对你有一定的参考价值。

<!DOCTYPE html>
<html>
    
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>DOM操作</title>    
    </head>
    <style>     
        #detail{
            width:200px;
            height:200px;
            border:1px solid black;     
            display :none;
            position:absolute;
            left:500px;
            top:300px;
            background:#fff;
        }
    </style>
    <body>
        标题:<input type="text" id="topic_name" size=60/><br>
        内容:<input type="text" id="topic_content" size=60/><br>
        作者:<input type="text" id="author" size=60/><br>
        <button id="saveBtn">保存</button>
        <table id="tab" border=1>
            <tr>
                <th>ID</th><th>帖子名称</th><th>内容预览</th><th>发布时间</th><th>作者</th><th>操作</th>
            </tr>
            <tr class="first">
                <td info="t">25</td>
                <td info="t" class="title">ABC</td>
                <td info="t">xxxxxxxx....</td>
                <td info="t">2016-04-15</td>
                <td info="t">LCE</td>
                <td name="option"><a name="detail" href="#">详细信息</a> <a class="delbtn" href="javascript:;">删除</a></td>
            </tr>
            <tr>
                <td info="t">25</td>
                <td info="t" class="title">ABC</td>
                <td info="t">xxxxxxxx....</td>
                <td info="t">2016-04-15</td>
                <td info="t">LCE</td>
                <td name="option"><a name="detail" href="#">详细信息</a> <a class="delbtn" href="javascript:;">删除</a></td>
            </tr>
            <tr>
                <td info="t">25</td>
                <td info="t" class="title">ABC</td>
                <td info="t">xxxxxxxx....</td>
                <td info="t">2016-04-15</td>
                <td info="t">LCE</td>
                <td name="option"><a name="detail" href="#">详细信息</a> <a class="delbtn" href="javascript:;">删除</a></td>
            </tr>
            <tr>
                <td info="t">25</td>
                <td info="t" class="title">ABC</td>
                <td info="t">xxxxxxxx....</td>
                <td info="t">2016-04-15</td>
                <td info="t">LCE</td>
                <td name="option"><a name="detail" href="#">详细信息</a> <a class="delbtn" href="javascript:;">删除</a></td>
            </tr>
            <tr>
                <td info="t">25</td>
                <td info="t" class="title">ABC</td>
                <td info="t">xxxxxxxx....</td>
                <td info="t">2016-04-15</td>
                <td info="t">LCE</td>
                <td name="option"><a name="detail" href="#">详细信息</a> <a class="delbtn" href="javascript:;">删除</a></td>
            </tr>
        <table>
        <div id="detail"></div>
    </body> 
</html>
<script src="public.js"></script>
<script>
    var tab = $id("tab");
    
    //添加保存按钮的点击事件
    $id("saveBtn").onclick = function(){
        //查找第一行
        var oTr = document.querySelector(".first");
        //克隆表格的第一行
        var cloneTr = oTr.cloneNode( true );
        cloneTr.children[0].innerHTML = rand(1,100);
        cloneTr.children[1].innerHTML = $id("topic_name").value;
        cloneTr.children[2].innerHTML = $id("topic_content").value;
        cloneTr.children[3].innerHTML = dateToString( new Date() );
        cloneTr.children[4].innerHTML = $id("author").value;
//      将克隆的行添加到table里
        tab.appendChild( cloneTr );
    }
//  给table添加点击事件
    tab.onclick = function(e){
        var e = e || event;
        var target = e.target || e.srcElement;
        //从新获取可以进行编辑的事件源
        if( target.getAttribute("info") == "t" ){
            //创建一个input
            var opt = create("input");
            opt.type = "text";  
            //将target的内容添加到文本框中
            opt.value = target.innerHTML;
            //然后清空target中的内容
            target.innerHTML = "";
            
            target.appendChild( opt );
            
            //让某个元素自动获取焦点
            opt.focus();//文本框自动获取焦点
                    
            //当文本框失去焦点时
            //将文本框的内容添加到target中
            opt.onblur = function(){
                target.innerHTML = this.value;
            }
        }
        //详细信息
        if( target.name == "detail" ){
            e.stopPropagation?e.stopPropagation():e.cancelBubble=true;
            $id("detail").style.display = "block";
            $id("detail").style.left = e.pageX + "px";
            $id("detail").style.top = e.pageY + "px";
            var title = target.parentNode.parentNode.children[1].innerHTML;
            var author = target.parentNode.parentNode.children[4].innerHTML;
            //显示详情
            $id("detail").innerHTML = "标题:" + title + "<br>作者:" + author;
        }
        //删除
        if( target.className == "delbtn" ){
            if( confirm("确定要删除?") ){
                target.parentNode.parentNode.remove();
            }
        }
    }
    //点击文档 隐藏详情
    document.onclick = function(){
        $id("detail").style.display = "none";
    }
</script>


































以上是关于委托应用-表单的创建和编辑的主要内容,如果未能解决你的问题,请参考以下文章

雷林鹏分享:jQuery EasyUI 应用 - 创建展开行明细编辑表单的 CRUD 应用

EmberJS:无需转换到新 URL 即可显示编辑/创建表单?

使用代码访问通过 LWUIT 资源编辑器创建的表单

Django:使用TemplateView在Django表单中添加编辑和删除功能的问题

如何将现成的项目(使用应用程序委托创建和启动但没有 xib、nib)添加到另一个项目

QT QTableView 使用委托