簡體   English   中英

如何在函數 [onClick ] 的表中添加新行,並從除新行之外的所有行中刪除此屬性

[英]how to add a new row in table in function [onClick ],and remove this attribute from the all row except the new

我編寫了一個添加新行的代碼,並在單擊表中的現有行時刪除了 (onClick) 屬性

<table style="vertical-align:middle;margin:20px;" id="table_insert">
 <tr  id="tra" >
  <td >Word :</td><td onClick="insert_tr()"> <input type="text" id='word_text' ></input></td>
  <td>Definition :</td><td><input type="text" id='def_text' ></input></td>
 </tr>
</table>

這是添加新行的函數,從前一個刪除 onclick attr 並將其添加到新添加的行:

<script type="text/javascript">
function insert_tr(){
     $(this).removeAttr('onClick'); //jQuery inside javascript function
     console.log("text:"+$(this).find('td').attr('onClick') ); 
        $('#table_insert').append('<tr onClick="insert_tr()"><td>Word :</td><td > <input type="text" id="word_text" ></input></td><td>Definition :</td><td><input type="text" id="def_text" ></input></td></tr>');
}
</script>

但它不起作用,在控制台屏幕上,它打印 [text:undefined] ..我該如何修復它?!!

$(this) -- 這是一個窗口對象,而不是您期望的元素

this傳遞給函數

onClick="insert_tr(this)"

/

function insert_tr(elem){
     $(elem).removeAttr('onClick'); //jQuery inside javascript function
     // Your code here
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM