【问题标题】:Uncaught ReferenceError: function is not defined error when calling riot function未捕获的 ReferenceError:调用 riot 函数时未定义函数错误
【发布时间】:2017-01-17 12:22:30
【问题描述】:

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

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

我正在尝试在鼠标点击时将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");
}

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

Uncaught ReferenceError: focusGone is not defined

如何在 riotjs 中完成这项工作?

【问题讨论】:

  • @Okazari 好的,谢谢!
  • setAttribute 设置为{focusGone} 时可能不起作用,这不是一个函数,而是某种可能更早运行的模板
  • @adeneo 我用customTextArea.setAttribute('onmouseout','focusGone()'); 试过,但也没有用

标签: javascript jquery riot.js riot


【解决方案1】:

您要在运行时更新标签定义,这是不受支持的 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/

【讨论】:

  • 但问题是在focusGone()函数内部编辑后无法获取值。检查这个小提琴:jsfiddle.net/1b2m7xec/2
  • 非常感谢。
猜你喜欢
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 2014-08-26
  • 2012-09-08
  • 1970-01-01
  • 2020-01-25
相关资源
最近更新 更多