我有一个工作的JS代码(当你点击一个id为#的单元格时,它会突出显示另一个表格中的单元格和相关行),我希望将它移动到外部JS .js文件这样我就可以重用其他PHP文件。
<td class="text-center" id="grid_B9">Click me to highlight me and other row</td>
<tr id="E3"><td class="text-center"></td>
$page_script .= "$(document).ready(function(){";
foreach ($storage_position_used as $storage_id => $positions)
// add class 'success' to table row
foreach ($positions as $storage_position_used) {
$page_script .= '$("#grid_'.$storage_position_used.'").click(function(){';
$page_script .= '$("#grid_'.$storage_position_used.'").toggleClass("success");';
$page_script .= '$("#'.$storage_position_used.'").toggleClass("success");';
$page_script .= "});";
}
$page_script .= "});";
到目前为止我在JS外部文件中重新编码,但是无法正常工作。有亮点,但总是在同一个单元格上。
$storage_position_used = storage_position_array($storage_id, 'used');
$page_script .= 'highlight_specimen_row('.json_encode($storage_position_used[$storage_id]).');';
// highlight_specimen_row(["E2","A3","A1","C1","B1","D1","C9"]);
function highlight_specimen_row (positions_used) {
$(document).ready(function(){
for (i = 0; i < positions_used.length; i++) {
var grid_position_id = "grid_"+positions_used[i];
var position_id = positions_used[i];
$('#'+grid_position_id).click(function() {
$('#'+grid_position_id).toggleClass("success");
$('#'+position_id).toggleClass("success");
});
}
});
}
我相信我错误地声明了我的动态变量(grid_position_id和position_id)。