单击跨度导致模糊事件移动跨度,以便它不会注册单击

时间:2015-01-30 15:18:20

标签: javascript dom

我在输入的右侧有一个跨度,其中包含一些文本。输入具有浏览器选择的默认宽度。输入有一个模糊处理程序,可将其转换为新的跨度,其文本与模糊输入中的文本相同。跨度具有相反的操作:单击处理程序将其转换为新输入,其文本与单击的跨度中的文本相同。

单击输入右侧的跨度时,输入会记录它的模糊事件,并按设计成为跨度。然而,它也变得更小(假设不是很多文本),这也是期望的。这使得单击的跨度向左移动。

问题:我们现在点击的原始范围可能不在鼠标指针下,也不再记录点击。

HTML:

<input id="write" class="tag" value="stuff"></input>
<span id="read" class="tag">some text</span>

js:

var write = document.getElementById("write");
var read = document.getElementById("read");

var writeOnBlur = function() {
    var newRead = document.createElement("span");
        newRead.className = "tag";
        newRead.innerHTML = this.value;
        newRead.onclick = readOnClick;
        this.parentNode.replaceChild(newRead, this);
        newRead.focus();
}

var readOnClick = function(e) {
    alert("clicked the 'read' node");

    var newWrite = document.createElement("input");
    newWrite.className = "tag";
    newWrite.value = this.innerHTML;
    newWrite.onblur = writeOnBlur;
    this.parentNode.replaceChild(newWrite, this);
    newWrite.focus();
    e.stopPropagation();
}

document.onclick = function() {
    alert("missed the read node. clicked the document.");
}

read.onclick = readOnClick;
write.onblur = writeOnBlur;
write.focus();

看到这个小提琴:http://jsfiddle.net/7s7kbvvf/10/

点击包含&#34;某些文字&#34;的范围看到问题。

2 个答案:

答案 0 :(得分:0)

根据您的需要更新了基于Javascript的解决方案:

   var switchToInput = function () {
      var input = document.createElement("input");
      input.className = "tag";
      input.type = "text";
      input.innerHTML = this.value;
      this.parentNode.replaceChild(input, this);
      input.onblur = switchToSpan;
      input.select();
  };
  var switchToSpan = function () {
      var span = document.createElement("span");
      span.className = "tag";
      span.innerHTML = this.value;
      this.parentNode.replaceChild(span, this);
      span.onclick = switchToInput;
  }
  var tags = document.getElementsByClassName('tag');
  for (var i = 0, limit = tags.length; i < limit; i++) {
      tags[i].onclick = switchToInput;
  }

更新小提琴:http://jsfiddle.net/7s7kbvvf/9/

答案 1 :(得分:0)

通过允许父节点(或者为了简化我的示例,文档)处理事件,我能够找到一个不错的解决方案。该文档跟踪当前&#34;写标签&#34; (输入),以便当&#34;读取标签&#34;单击(span),它可以先用新输入替换span,然后用新的span替换当前输入,只需单击一下。由于跨度首先被新输入替换,因此一旦现有输入变为新跨度,它就会移动并不重要。

新的js(或参见此fiddle):

var write = document.getElementById("write");
var read = document.getElementById("read");
var currWrite = write;
write.focus();

document.onclick = function(e) {
    var target = e.target;
    if (target && target.nodeName === "SPAN") {
        var newWrite = document.createElement("input");
        newWrite.className = "tag";
        newWrite.value = target.innerHTML;
        target.parentNode.replaceChild(newWrite, target);
    }
    if (currWrite !== null) {
        var newRead = document.createElement("span");
        newRead.className = "tag";
        newRead.innerHTML = currWrite.value;
        currWrite.parentNode.replaceChild(newRead, currWrite);
        currWrite = null;
    }
    if (newWrite) {
        currWrite = newWrite;
        newWrite.focus();
    }

}