我将数据插入HTML,如下所示:

<p each="{this.holidayListFirstPart}" if="{hdate}">
    <span id="{description}" onclick={showInputBox}>{hdate}:{description}</span>
</p>


我正在尝试在mouseclick上将span标记转换为textarea,以便用户可以像这样编辑文本:

showInputBox(e) {
    self.textContent = document.getElementById(e.target.id).innerHTML;

    var mySpan = document.getElementById(e.target.id);
    var customTextArea = document.createElement("textarea");
    customTextArea.id = e.target.id;
    customTextArea.setAttribute('onmouseout','{focusGone}');
    customTextArea.innerHTML = self.textContent;
    mySpan.parentNode.replaceChild(customTextArea, mySpan);
}

focusGone(e){
    console.log("lost focus");
}


问题是当用户在编辑文本后离开文本区域时,未定义focusGone函数的抛出错误:

Uncaught ReferenceError: focusGone is not defined


如何在riotjs中进行这项工作?

最佳答案

您想在运行时更新标签定义,但不支持
https://github.com/riot/riot/issues/1752

但是您可以通过其他方式获得相同的结果

<my-tag>
<my-tag>
  <p each="{this.holidayListFirstPart}" if="{hdate}">
        <span show="{!parent.editing}" id="{description}" onclick={showInputBox}>{hdate}:{description}</span>
        <textarea id="editText" onmouseout="{parent.focusGone}" show="{parent.editing}"></textarea>
  </p>

  this.holidayListFirstPart = [{description:'des1', hdate:'123'}, {description:'des2'}]
  this.editing = false

  showInputBox(e) {
      this.editing = !this.editing
      this.editText.innerText = e.currentTarget.innerText
  }

  focusGone(e){
    this.editText.innerHTML = e.currentTarget.value
    alert('result: ' + this.editText.innerHTML);
  }
</my-tag>


更新资料
我根据您的评论更新了代码。这个想法是要知道如何访问所需的数据,您可以使用event.currentTarget或直接使用this.object_id
检查这个小提琴https://jsfiddle.net/vitomd/1b2m7xec/6/

09-20 03:36