我无法检索第一个单元格的内容。我正在尝试这样:
console.log($(this).closest("tr").find('td:first').innerHTML);
//or...
console.log($('td:first', $(this).parents('tr')).text());
代码:
$('.editable').click(function () {
var text = $(this).text();
if (typeof $(this).find("textarea")[0]=="undefined")//checking if we have a textarea already
{
$(this).text('');
$('<textarea />').appendTo($(this)).val(text).select().blur(function () {
var newText = $(this).val();
$(this).parent().text(newText).find('textarea').remove();
console.log($(this).closest("tr").find('td:first').innerHTML);
});
}
});
JSfiddle:http://jsfiddle.net/KjknL/
我认为问题与动态添加的文本区域有关。但我无法摆脱它。如果我使用 .prev() 或 .next(),它返回空。有什么想法吗?
最佳答案
试试这个:http://jsfiddle.net/KjknL/2/
textarea 不再存在,因为您在尝试从它到达第一个 td 之前将其删除。
console.log($(this).closest("tr").find('td:first').html());
$(this).parent().text(newText).find('textarea').remove();
关于javascript - 从另一个单元格内动态创建的元素获取第一个单元格的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23676826/