我有一个表,我想通过点击一个href标签删除tr.这是代码.
<tr>
<td data-title="ID">
echo '<a href="" onclick="return Deleteqry('.$orderID.')";><font size="2" color="#FF0000"><i class=" fa fa-remove" title="Remove this Row"> </i></a>';
</td>
</tr>
<script>
function Deleteqry(id)
{
if(confirm("Are you sure you want to delete this Row?")==true)
$(this).closest('tr').remove();
return false;
}
</script>
Run Code Online (Sandbox Code Playgroud)
我在SO中检查了其他问题$(this).closest('tr').remove();但发现 它不起作用.我没有得到任何错误,但行没有删除.我正在收到警报框,但行没有删除.
请告诉我我做错了什么.
基本上this在正常函数内部将指向window对象.
.... onclick="return Deleteqry('.$orderID.', this)";>
Run Code Online (Sandbox Code Playgroud)
所以你必须手动将它传递给内联事件处理函数.
function Deleteqry(id,_this) {
if(confirm("Are you sure you want to delete this Row?")==true)
$(_this).closest('tr').remove();
return false;
}
Run Code Online (Sandbox Code Playgroud)
接收它,将其转换为jquery对象并完成它的工作.