我有以下結構的HTML表:如何引用從另一個在JQuery中相同<tr>內的一個?
<tr>
<td>123</td>
<td ondblclick="makeeditable(this);">this is some text</td>
<td><span ondblclick="makeeditable(this);">this is some more text</span><span>flag</span></td>
</tr>
我寫一個jQuery片段,使第二<td>
和第三<td>
用戶可編輯的第一<span>
用雙擊(對於它的價值,該表正在動態生成):
function makeeditable(cell){
var OriginalContent = $(cell).text();
$(cell).html("<input id='editcell' class='input' type='text' value='" + OriginalContent + "' />");
$(cell).children().first().focus();
$(cell).children().first().keypress(function (e) {
if (e.which == 13) {
var newContent = $(this).val();
$(this).parent().text(newContent);
}
});
$(cell).children().first().blur(function(){
$(this).parent().text(OriginalContent);
$(this).parent().removeClass("cellEditing");
});
}
使用上面的函數,我成功地使單元格可編輯。不過,現在我需要以某種方式檢索剛編輯的單元格的行參考號(第一<td>
內的文本,在這個例子中)。我的問題是,怎麼能夠從那個內同一行的另一個<td>
一個<span>
的同一排和第二<td>
的情況下引用行的第一<td>
?
jQuery有一個兄弟姐妹功能:https://api.jquery.com/siblings/ – tkausl
'變種父母= $(小區).parent( 'TR'); VAR第一= parent.children( 'TD')[0]; first.innerText =「abc」;' – puemos