【问题标题】:Change element by click like PHPAdmin通过点击更改元素,如 PHPMYAdmin
【发布时间】:2012-12-21 23:57:46
【问题描述】:

我是 js 的初学者。我想做一个像 PHPAdmin 这样的编辑器。单击其表格时,该字段将变为文本区域。当点击文本区域之外的其他地方时,它将变回原文件并执行sql。

以下是我想用jQuery编写的,我完全不明白我应该如何进一步编码,请指教。

$('#editor #gird_edit').bind({
  click: function() { //When Click
      var content = $(this).text(); // read what is in the filed
      $("#gird_edit").text('<textarea>'+a+'</textarea>'); // This is not work, will only add html code,not change to text-area
  },
  /* ??? */: function() { //Outside click of the text-area 
      var content = $(this).text(); // read what is in the text-area
      $("#gird_edit").text(????);  // change back to the filed 
  }
})

HTML

<div id='editor'>
   <div id='gird_edit'>hallo world</div>
   <div id='gird_edit'>hallo world 2</div>
   <div id='gird_edit'>hallo world 3</div>
</div>

我只有 3 个声望,昨天才加入...很抱歉我不能投票给你,因为它需要 15 个声望。但是,我将非常感谢您的帮助!

【问题讨论】:

  • 您不能在多个 DIV 上使用 id='gird_edit',因为 ID 必须是唯一的;您可能应该使用类而不是 ID。关于您的总体目标,请查看Jeditable plugin

标签: javascript jquery forms text phpmyadmin


【解决方案1】:

如果您想检测元素外部的点击,只需在整个页面上检测它们,并丢弃来自元素内部的任何点击。换句话说:

$('body').on('click', : function(e) { //Outside click of the text-area 
    if ($(this).parents().is('#gird_edit')) return false;
    var content = $('textarea').text(); // read what is in the text-area
    $("#gird_edit").text(????);  // change back to the filed 
});

但是,听起来您真正要寻找的是“模糊”处理程序,只要有人在文本区域内并离开它,就会触发它;您可以使用与您制作点击处理程序相同的基本方式来制作其中一种:

$('#gird_edit textarea').bind({
    blur: function() {
        // do the reverse of the click handler
}

【讨论】:

  • 这行得通!非常感谢!!还有一个问题,当我尝试将 div 更改为 text-area 时, $("#gird_edit").text('');只会输出html,不会将视觉更改为文本区域,我该怎么办?
  • 使用.html() 而不是.text()
  • 对不起,它运行得不好....我有什么问题吗? $('#editor #gird_edit').bind({ click: function() { $(this).html(''); }, 模糊:function() { $(this).html('
    '+$(this).text()+'
    '); } }) ;
  • 因此,请在 Stack Overflow 上快速了解一下礼仪:如果您有一个新问题,最好将其作为一个新问题提出,而不是在 cmets 中提出。 SO 的创建者故意将 cmets 设计为比问题/答案更尴尬以鼓励这一点。如果您在有关 .text/.html 的问题中发布了一个新的 Stack Overflow 问题,我敢肯定,我自己、Barmar 和这里的其他所有人都会很快得到答案:-)
猜你喜欢
  • 1970-01-01
  • 2011-07-20
  • 1970-01-01
  • 2015-06-21
  • 1970-01-01
  • 2020-04-09
  • 1970-01-01
  • 2017-10-09
  • 1970-01-01
相关资源
最近更新 更多