我是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个声誉。但是,我将非常感谢你的帮助!!
答案 0 :(得分:2)
如果要检测元素外部的点击次数,只需在整个页面上检测它们,然后扔掉任何来自元素内部的点击。换句话说:
$('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
});
然而,听起来你真正想要的是一个“模糊”处理程序,只要有人在textarea中并且离开它就会触发;您可以使用与点击处理程序相同的基本方法制作其中一种:
$('#gird_edit textarea').bind({
blur: function() {
// do the reverse of the click handler
}