<tbody id="content-tbody"> <tr> <th style="height: 60px">' + item.id + '</th> <th style="height: 60px">' + item.sort + '</th> <th style="height: 60px">' + item.title + '</th> <th style="height: 60px">' + item.type + '</th> <th style="height: 60px">' + item.title + '</th> <th style="height: 60px">' + item.time + '</th> <th style="height: 60px"> <button class="btn" οnclick="newsUpd(this)" style="width: 36%;height: 40px">编辑 </button> <button class="btn" οnclick="newsDel(this)" style="width: 36%;height: 40px">删除 </button> </th> </tr> </tbody>
function newsUpd(data) { var id = $(data).parent().parent().children().eq(0).text(); var oldSort = $(data).parent().parent().children().eq(1).text(); var oldTitle = $(data).parent().parent().children().eq(2).text(); var oldType = $(data).parent().parent().children().eq(3).text(); var oldContent = $(data).parent().parent().children().eq(4).text(); }
注意:
$(data).parent()是tr
$(data).parent().parent()是tbody
$(data).parent().parent().children()是所有tbody下的子元素id、oldSort、oldTitle、oldType、 oldContent
$(data).parent().parent().children().eq(0)是取的eq(index)index第0个位置的子元素id
需要注意:
在ajax方法里写$(this)指向的是最近调用它的jquery对象,所以这里的$(this)指的是ajax对象,
而不是$(".enter_caozuo").find(".gd"),要想解决这个问题
就需考虑增加一个中间变量传递这个对象
在ajax外面弄个变量var mythis = $(this),然后在里面用就行了