Javascript 如何使用regex for jquery选择器将某些属性应用于元素?
我有一个编辑按钮,应该使该表的内容可编辑。按钮id和类如下所示Javascript 如何使用regex for jquery选择器将某些属性应用于元素?,javascript,jquery,css,html-table,Javascript,Jquery,Css,Html Table,我有一个编辑按钮,应该使该表的内容可编辑。按钮id和类如下所示 id="edit_icon_'.$row1["client_id"].'_'.$row2["project_id"].'" class="editbtn" Id取决于db值。单击此按钮后,我希望表的所有元素“仅一个表”都可以编辑 <tr> <td id="edit_elem_'.$row2["project_id"].'_'.$row3["detail_id"].'">'.$row3["elemen
id="edit_icon_'.$row1["client_id"].'_'.$row2["project_id"].'"
class="editbtn"
Id取决于db值。单击此按钮后,我希望表的所有元素“仅一个表”都可以编辑
<tr>
<td id="edit_elem_'.$row2["project_id"].'_'.$row3["detail_id"].'">'.$row3["elements"].'</td>
<td id="edit_respon_'.$row2["project_id"].'_'.$row3["detail_id"].'">'.$row3["responsibilty"].'</td>
<td id="edit_remark_'.$row2["project_id"].'_'.$row3["detail_id"].'">'.$row3["remarks"].'</td>
</tr>
编辑:p.S.我在同一页上生成了多个表 你不需要正则表达式。 您可以使用
data-
属性来存储编辑按钮要编辑的id
按钮:
<button class='editclick' data-pid="124" data-did="1">Edit</td>
JavaScript:
$(document).on('click', '.editclick', function() { //entering edit mode
var cid = $(this).data('did'); // 1
var pid = num = $(this).data('pid') // 124
$("td.d"+did+".p"+pid).attr('contenteditable','true');
});
不要做这么复杂的ID。在元素上使用一个类并给它们一些数据属性。我所展示的表只是一个片段。该页面包含几个生成的表,这些表的ID或类与我从db附加的变量不同。@VPR上述代码即使在多个表中也能工作。您知道事件委托在JavaScript中是如何工作的吗?@YaronU。工作起来很有魅力。
<tr>
<td id="edit_elem_'.$row2["project_id"].'_'.$row3["detail_id"].'" class="p'.$row2["project_id"].' d'.$row3["detail_id"].'">'.$row3["elements"].'</td>
<td id="edit_respon_'.$row2["project_id"].'_'.$row3["detail_id"].'" class="p'.$row2["project_id"].' d'.$row3["detail_id"].'">'.$row3["responsibilty"].'</td>
<td id="edit_remark_'.$row2["project_id"].'_'.$row3["detail_id"].'" class="p'.$row2["project_id"].' d'.$row3["detail_id"].'">'.$row3["remarks"].'</td>
</tr>
$(document).on('click', '.editclick', function() { //entering edit mode
var cid = $(this).data('did'); // 1
var pid = num = $(this).data('pid') // 124
$("td.d"+did+".p"+pid).attr('contenteditable','true');
});