yy_shine1 2021-11-15 18:59 采纳率: 100%
浏览 44
已结题

双击文本框出现空白,阻止双击事件没用

我想要的效果是双击选项,出现文本框,以达到修改选项内容的目的。可是我在双击文本框后,发现文本框里的内容变成空白了,试过屏蔽input的双击事件也没用。后面是完整代码。

img

img

img

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        input {
            margin: 0;
            padding: 0;
        }
        
        div,
        ul,
        li,
        button,
        h4 {
            margin: 0;
            padding: 0;
        }
        
        #oopTab {
            width: 800px;
            height: 500px;
            /* border: 1px solid pink; */
            margin: 200px auto 0px
        }
        
        #oopTab h3 {
            text-align: center;
            font-weight: 700;
        }
        
        .tab {
            border: 1px solid pink;
        }
        
        .tab ul {
            margin-top: -1px;
            height: 60px;
        }
        
        .tab li {
            position: relative;
            list-style: none;
            float: left;
            height: 60px;
            padding: 0px 10px;
            border-right: 1px solid pink;
            /* border-bottom: 1px solid pink; */
            text-align: center;
            font-size: 18px;
            cursor: default;
        }
        
        .tab li h4 {
            line-height: 60px;
        }
        
        .tab li input {
            width: 80px;
            height: 30px;
            outline-color: #22A4FF;
        }
        
        .tab li button {
            position: absolute;
            top: 0px;
            right: 0px;
            background-color: #000;
            color: #fff;
            border-radius: 0 0 0 30px;
            font-size: 12px;
            padding-left: 5px;
        }
        
        .tab ul div {
            float: right;
            height: 60px;
        }
        
        .tab section {
            height: 400px;
            margin-top: 1px;
            border-top: 1px solid pink;
            padding: 10px;
            margin: -1px;
        }
        
        .tab section div {
            display: none;
            background-color: pink;
            word-wrap: break-word;
            word-break: normal;
        }
        
        .tab .add {
            float: right;
            width: 30px;
            height: 30px;
            background-color: #fff;
            border: 1px solid #929292;
            margin: 15px;
        }
        
        .liactive {
            border-bottom: 1px solid #fff;
        }
        
        .tab .isblock {
            display: block;
        }
    </style>
</head>

<body>
    <div id="oopTab">
        <h3>js面向对象动态增加标签页</h3>
        <div class="tab">
            <ul>
                <li class="liactive">
                    <h4>测试1</h4>
                    <button>X</button>
                </li>
                <li>
                    <h4>测试2</h4>
                    <button>X</button>
                </li>
                <li>
                    <h4>测试3</h4>
                    <button>X</button>
                </li>
                <div><button class="add">+</button></div>
            </ul>
            <section>
                <div class="isblock">
                    1
                </div>
                <div>
                    2
                </div>
                <div>
                    3
                </div>
            </section>
        </div>
    </div>
    <script>
        var that = null;
        class Tab {
            constructor(id) {
                that = this;
                this.main = document.querySelector(id);
                this.add = this.main.querySelector(".add");
                this.ul = this.main.querySelector(".tab ul");
                this.section = this.main.querySelector(".tab section");
                this.init();
            }
            init() {
                this.upadteNode();
                this.add.onclick = this.addTab;
                for (var i = 0; i < this.lis.length; i++) {
                    this.lis[i].index = i;
                    this.lis[i].onclick = this.toggleTab;
                    this.remove[i].onclick = this.removeTab;
                    this.h4[i].ondblclick = this.edit;
                }
            }
            upadteNode() {
                this.lis = this.main.querySelectorAll("li");
                this.sections = this.main.querySelectorAll(".tab section div");
                this.remove = this.main.querySelectorAll("li button");
                this.h4 = this.main.querySelectorAll("li h4");
            }

            addTab() {
                that.clearClass();
                var li = '<li class="liactive"><h4>新的选项卡</h4><button>X</button></li>';
                var section = '<div class="isblock">新</div>';
                that.ul.insertAdjacentHTML('beforeend', li);
                that.section.insertAdjacentHTML('beforeend', section);
                that.init();
            }
            toggleTab() {
                that.clearClass();
                this.className = "liactive";
                that.sections[this.index].className = "isblock";
            }
            clearClass() {
                for (var i = 0; i < this.lis.length; i++) {
                    this.lis[i].className = "";
                    this.sections[i].className = "";
                }
            }
            removeTab(e) {
                //阻止冒泡
                e.stopPropagation();
                var index = this.parentNode.index;
                that.lis[index].remove();
                that.sections[index].remove();
                that.init();
                if (document.querySelector(".licative")) return;
                index--;
                that.lis[index] && that.lis[index].click();
            }
            edit(e) {
                e.stopImmediatePropagation();
                var str = this.innerText;
                window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
                this.innerHTML = '<input type="text">';
                var input = this.children[0];
                input.value = str;
                this.οndblclick = "return false";
                input.οndblclick = "return false";
            }

        }
        var tab = new Tab("#oopTab");
    </script>
</body>

</html>

  • 写回答

2条回答 默认 最新

  • CSDN专家-showbo 2021-11-15 21:25
    关注

    题主什么编辑器啊?edit事件中o字母已经不是o字母了。。虽然网页显示上看起来一样,但是在编辑器中有明显区别的

    img

    
                edit(e) {
                    e.stopImmediatePropagation();
                    var str = this.innerText;
                    window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
                    this.innerHTML = '<input type="text">';
                    var input = this.children[0];
                    input.value = str;
                   // this.οndblclick = "return false";
                    // this.οndblclick = "return false";
                    //==>>o字母输入有问题,改成下面的就正常了,再次双击不会清空内容
                    input.ondblclick = "return false";
                    input.ondblclick = "return false";
                }
    
    

    img


    有帮助麻烦点下【采纳该答案】,谢谢~~

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月24日
  • 已采纳回答 11月16日
  • 创建了问题 11月15日

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里