事件委托 tr上 tr的display问题

解决tr元素事件委托难题
本文探讨了在使用事件委托时遇到的tr元素问题,解释了为何直接在tr上添加事件监听器会失败,并提供了解决方案:通过在tr内部添加div元素来成功捕获事件。同时,文章还提到了tr元素的显示属性设置技巧,避免表格错行。

今天想给tr添加一个事件,事件委托实现,发现怎么也捕获不到tr

查了好多,说tr不是盒子模式,也不是很懂,反正就是不行

解决办法是在tr里面加一个div,去捕获这个div就可以了。

有知道什么原因的小伙伴们可以多多留言谢谢。

 

如果你要给tr设置display 为block会发现表格会错行,感觉tr好多bug........

给tr  display 设置为空就可以了。

 

如果你想给tr一行加个事件,比如说点击一整行出现什么的,事件委托的时候不要给tr加,给tr的每个td加就可以了。要不然你会发现tr不能触发事件。感觉tr好多bug........

 

 

<!DOCTYPE html> {% load static %} <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="icon" href="{% static 'publicStatic/images/favicon.ico' %}"> <link rel="stylesheet" href="{% static "css/font-awesome.min.css" %}"> {#<link href="{% static "bootstrap/css/bootstrap.css" %}" rel="stylesheet" type="text/css">#} <!-- bootstrap-theme.css 需要屏蔽掉,否则和bootstrap.css冲突,造成 下拉菜单悬浮 的子菜单项背景色不起作用 --> <link href="{% static "bootstrap/css/bootstrap-theme.css" %}" rel="stylesheet" type="text/css"> {# <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">#} <link rel="stylesheet" href="{% static "bootstrap/css/bootstrap.min.css" %}" type="text/css" > <script src="{% static "bootstrap/jquery-1.9.1.js" %}"></script> <script src="{% static "bootstrap/js/bootstrap.js" %}"></script> <!--20250928添加bootstrap.bundle.min.js,否则当鼠标悬停在‘项目管理’下面的二级菜单,不会显示二级菜单下的三件菜单 另外,这里添加之后,打开网页必须进行 ctr+F5 刷新一次页面才可以,否则无效果--> <script src="{% static "sweetalert/sweetalert.min.js" %}" ></script> <link rel="stylesheet" href="{% static "sweetalert/sweetalert.min.css" %}" > <script src="{% static "bootstrap/js/bootstrap.bundle.min.js" %}"></script> <script type="text/javascript" src="{%static 'datepicker/js/bootstrap-datepicker.js' %}" ></script> <link rel="stylesheet" type="text/css" href="{% static 'datepicker/css/datepicker.css' %}"> </head> <body> <!-- 工单表格 --> <table class="table table-hover" name="processedorder" id="processedorder"> <thead> <tr> <th>序号</th> <th>工作令号</th> <th>名称</th> <th>图号</th> <th>数量</th> <th>要求完成时间</th> <th>图纸数量</th> <th>整件号</th> </tr> </thead> <tbody> <!-- 动态生成的行将在这里添加 --> </tbody> </table> <!-- 设备表格 --> <P class="text-center"><b>使用设备</b></P> <table class="table table-bordered table-hover text-center" name="device" id="device"> <thead> <tr> <th>设备ID</th> <th>设备名称</th> <th>序列号</th> <th>开始时间</th> <th>截止时间</th> <th>操作</th> </tr> </thead> <tbody> <!-- 设备行将在这里添加 --> </tbody> </table> <!-- 控制按钮 --> <button id="button_processedorder">添加工单行</button> <button id="button_device">选择设备</button> <button id="button_save">保存工单</button> <!-- 设备选择模态框 --> <div id="device_modal" class="modal fade" style="display: none;"> <!-- 模态框内容 --> <div class="modal-dialog"> <div class="modal-content"> <!-- 模态框头部 --> <div class="modal-header"> <h4 class="modal-title">仪器一览表</h4> <button type="button" class="close" data-dismiss="modal">×</button> </div> <!-- 模态框主体 --> <div class="modal-body"> <table id="device_table"> <!-- 设备数据将在这里动态加载 --> </table> </div> <!-- 模态框底部 --> <div class="modal-footer"> <button type="button" class="btn btn-secondary close" data-dismiss="modal">关闭</button> </div> </div> </div> </div> <script> $(document).ready(function() { function safeInitDatepicker(element) { const dateStr = element.text().trim(); if (!dateStr) { element.text(new Date().toISOString().split('T')[0]); } else if (!/^\d{4}-\d{2}-\d{2}$/.test(dateStr)) { element.text(new Date(dateStr).toISOString().split('T')[0]); } element.datepicker({ format: 'yyyy-mm-dd', autoclose: true, startDate: new Date() }); } // 假设模态框的 ID 是 deviceModal $('table#processedorder td').attr('contenteditable', true); $('table#device td').attr('contenteditable', true); <!--解决模态点击关闭,无法关闭的问题--> $('.close').click(function() { $('#device_modal').modal('hide'); }); // 选择设备按钮 $('#button_device').click(function() { $.get('/project/get_devices/', function(response) { let html = ''; {#alert('11111111111111');#} if (response && response.devices) { // 添加空值检查 $.each(response.devices, function(i, device) { html += `<tr> <td>${device.id}</td> <td>${device.name}</td> <td>${device.serial}</td> <td><button class="select-device" data-id="${device.id}">选择</button></td> </tr>`; }); {#alert(html)#} $('#device_table').html(html); $('#device_modal').modal('show'); {#alert('33333333333');#} } else { console.error('设备数据格式错误:', response); } }); // 设备选择事件 $(document).on('click', '.select-device', function(event) { event.preventDefault(); let selectedDevice = $(this).data('id').toString(); {#alert(selectedDevice)#} let exists = false; $('#device tbody tr').each(function () { if ($(this).find('td:first').text() === selectedDevice) { exists = true; alert('有重复,请选择其它') return false; // 停止遍历 } }); {#alert(exists)#} const deviceId = $(this).data('id'); $.get(`/project/get_device_details/${deviceId}/`, function(device) { if (!exists) { $('#device tbody').append(` <tr data-id="${device.id}"> <td>${device.id}</td> <td>${device.name}</td> <td>${device.serial}</td> <td class="datepicker" >${device.current_date}</td> <td class="datepicker" >${device.current_date}</td> <td><button class="delete-item" >删除</button></td> </tr> `); } $(this).closest('tr').remove(); <!--必须要有下面这条指令,否则不能编辑--> // 设置倒数第一列和倒数第二列可编辑 // 为新添加的行设置可编辑和绑定 datepicker $('#device tbody tr:last').find('td').eq(-2).attr('contenteditable', true); // 倒数第一列 $('#device tbody tr:last').find('td').eq(-3).attr('contenteditable', true); // 倒数第二列 // 绑定删除事件(使用事件委托) $('#device tbody tr').on('click', '.delete-item', function() { $(this).closest('tr').remove();}); {#const newRow = $('#device tbody tr:last');#} {#newRow.find('.datepicker').each(function() {#} {# initDatepicker($(this));});#} {#$('#device tbody tr:last').find('td.datepicker').datepicker({#} {# format: 'yyyy-mm-dd', // 确保年份显示#} {# autoclose: true,#} {# defaultDate: new Date()});#} }); }); }); }); </script> </body> </html> 当关闭<div id="device_modal"之后,再次点击‘选择设备’按钮,则在<div id="device_modal" 点击‘选择’一条记录时候,会在前面的<table class="table table-bordered table-hover text-center" name="device" id="device"> 生产2条记录,当再次关闭 model,再次打开,每次回生产3次记录
09-30
根据下面的页面代码,需要编写这个js点击事件,点击明细表中每行的name为extendDataFormInfo.value(fd_3e59465a09231a.0.fd_3e5946689e29d2)的输入框,能跳转到对应这个输入框的value里面的链接地址,页面代码如下:<tr> <td row="2" column="0,1,2,3" class="td_normal_title" colspan="4" style="width: auto;" width="undefined"> <script> $(function() { var tb = document.getElementById('TABLE_DL_fd_3e59465a09231a'); tb.setAttribute('tbdraggable', 'true'); }); </script> <script> Com_IncludeFile('doclist.js'); </script> <script id="doclist_js" src="/resource/js/doclist.js?s_cache=1754870647847"></script> <script id="doclistdnd_js" src="/resource/js/doclistdnd.js?s_cache=1754870647847"></script> <script> DocList_Info.push('TABLE_DL_fd_3e59465a09231a'); </script> <script> Com_IncludeFile('detailsTableFreeze.css', Com_Parameter.ContextPath + 'sys/xform/designer/resource/css/', 'css', true); </script> <link rel="stylesheet" href="/sys/xform/designer/resource/css/detailsTableFreeze.css?s_cache=1754870647847"> <script> Com_IncludeFile('detailsTableFreeze.js', Com_Parameter.ContextPath + 'sys/xform/designer/resource/js/', 'js', true); </script> <script id="detailsTableFreeze_js" src="/sys/xform/designer/resource/js/detailsTableFreeze.js?s_cache=1754870647847"></script> <script> Com_AddEventListener(window, 'load', function() { setTimeout(function() { for (var i = 0; i < 1; i++) { DocList_AddRow(document.getElementById('TABLE_DL_fd_3e59465a09231a')) }; tableFreezeStarter('TABLE_DL_fd_3e59465a09231a', false, undefined, false, true, 'add'); }, 500); }); </script> <table label="明细表1" id="TABLE_DL_fd_3e59465a09231a" align="center" class="tb_normal" tablename="fd_3e5946528a2834" style="width: 100%;" data-multihead="false" width="100%" showindex="true" showrow="1" showstatisticrow="undefined" showcopyopt="undefined" dataentrymode="multipleRow" required="undefined" performance="undefined" excelexport="undefined" excelimport="undefined" defaultfreezetitle="undefined" defaultfreezecol="undefined" layout2col="undefined" fd_type="detailsTable" tbdraggable="true"> <tbody> <tr class="tr_normal_title" type="titleRow" style="height: 33px;" invalidrow="true"> <td row="1" column="0" align="center" coltype="selectCol" style="width: 15px;"></td> <td row="0" column="1" align="center" class="td_normal_title" coltype="noTitle" style="width: 25px; white-space: nowrap;">序号</td> <td row="0" column="2" align="center" class="td_normal_title"> <label class="xform_label" line="normal" fd_type="textLabel" style="display: inline-block; width: auto; word-break: break-all; overflow-wrap: break-word;;word-break:break-all;;word-wrap:break-word;">文本1</label> </td> <td row="0" column="3" align="center" class="td_normal_title"> <label class="xform_label" line="normal" style="display: inline-block; width: auto; word-break: break-all; overflow-wrap: break-word;;word-break:break-all;;word-wrap:break-word;" fd_type="textLabel">文本3</label> </td> <td row="0" column="4" align="center" class="td_normal_title"></td> <td row="0" column="5" align="center" class="td_normal_title" coltype="blankTitleCol" style="width:48px;"></td> </tr> <tr class="" trdraggable="true" style="cursor: move; user-select: none;"> <td class="" align="center" valign=""><input type="checkbox" name="DocList_Selected" onclick="DocList_SelectRow(this);"></td> <td class="" align="center" valign="">1</td> <td class="" align="center" valign=""> <div class="xform_inputText" canshow="true" fd_type="inputText" style="display: inline-block; width: 147px; word-break: break-all; overflow-wrap: break-word;"> <xformflag flagid="fd_3e59465a09231a.0.fd_3e59465b4a38ce" id="_xform_extendDataFormInfo.value(fd_3e59465a09231a.0.fd_3e59465b4a38ce)" property="extendDataFormInfo.value(fd_3e59465a09231a.0.fd_3e59465b4a38ce)" flagtype="xform_text" _xform_type="text"><input name="extendDataFormInfo.value(fd_3e59465a09231a.0.fd_3e59465b4a38ce)" subject="文本1" title="文本1" onblur="__xformDispatch(this.value, this);" class="inputsgl xform_inputText" value="" type="text" style="display: inline-block; width: 147px; word-break: break-all; overflow-wrap: break-word;width:120px;"> </xformflag> </div> <input name="extendDataFormInfo.value(fd_3e59465a09231a.0.fdId)" value="" type="hidden"> </td> <td class="" align="center" valign=""> <div style="display: inline-block; width: 202px;width: 210px;" class="xform_formula_load"> <xformflag flagid="fd_3e59465a09231a.0.fd_3e5946689e29d2" id="_xform_extendDataFormInfo.value(fd_3e59465a09231a.0.fd_3e5946689e29d2)" property="extendDataFormInfo.value(fd_3e59465a09231a.0.fd_3e5946689e29d2)" flagtype="formula_calculation" _xform_type="formulaCalculation"> <script> Com_IncludeFile('formula_calculation_script.js', '../sys/xform/designer/formula_calculation/'); </script> <script id="formula_calculation_script_js" src="/resource/../sys/xform/designer/formula_calculation/formula_calculation_script.js?s_cache=1754870647847"> </script> <input name="extendDataFormInfo.value(fd_3e59465a09231a.0.fd_3e5946689e29d2)" value="http://192.1.1.92:8080/webroot/decision/view/duchamp?viewlet=crm_project%252F%25E7%2589%25A9%25E6%2596%2599%25E4%25BE%259B%25E9%259C%2580%25E6%259F%25A5%25E8%25AF%25A2%25E9%25A1%25B5%25E9%259D%25A2.fvs&ref_t=design&ref_c=1e33972c-8151-43ff-81bb-d263b846af18&page_number=1&p_material=123" type="hidden" loadtype="autoLoad,manualLoad"><input name="extendDataFormInfo.value(fd_3e59465a09231a.0.fd_3e5946689e29d2_text)" subject="文本3" title="文本3" formula_calculation="true" isrow="true" returntype="text" loadtype="autoLoad,manualLoad" controlsid="fd_3e59465a09231a.fd_3e59465b4a38ce" fdid="fd_3e59465a09231a.0.fd_3e5946689e29d2" right="" readonly="readOnly" class="inputsgl xform_formula_load" value="" type="text" style="display: inline-block; width: 202px;width: 200px;"> </xformflag> </div> </td> <td class="" align="center" valign=""> <label class="xform_linkLabel" fd_type="linkLabel" style="display: inline-block; "><a target="_blank" href="www.baidu.com" style="color: rgb(66, 133, 244); font-weight: normal; font-style: normal; text-decoration: none;" parent_style="display: inline-block; width: 58px;" parent_class="xform_linkLabel">超链接1 </a></label> </td> <td class="" align="center" valign=""> <nobr><span style="cursor:pointer" class="optStyle opt_copy_style" title="复制行" onmousedown="DocList_CopyRow();"></span>  <span style="cursor:pointer" class="optStyle opt_del_style" title="删除行" onmousedown="DocList_DeleteRow_ClearLast();XFom_RestValidationElements();"></span>   </nobr> </td> </tr> <tr type="statisticRow" invalidrow="true"> <td row="1" column="0" align="center" coltype="selectCol" style="width: 15px;"></td> <td row="2" column="1" align="center" coltype="noFoot" style="width: 25px;"> </td> <td row="2" column="2" align="center"> </td> <td row="2" column="3" align="center"> </td> <td row="2" column="4" align="center"> </td> <td row="2" column="5" align="center" coltype="emptyCell" style="width: 48px;"> </td> </tr> <tr type="optRow" class="tr_normal_opt" invalidrow="true"> <td row="3" column="0" align="center" coltype="optCol" colspan="6" style=""> <div class="tr_normal_opt_content" style="min-width:580px;;"> <div class="tr_normal_opt_l"><label class="opt_ck_style" style="position:relative;top:3px;"><input type="checkbox" name="DocList_SelectAll" onclick="DocList_SelectAllRow(this);"><span style="margin-left: 6px;">全选<span></span></span></label><span style="margin-left:15px;" onclick="DocList_BatchDeleteRow();XFom_RestValidationElements();"><span class="optStyle opt_batchDel_style" style="margin-left:0px; " title="删除行"></span><span style="position:relative;top:3px;cursor: pointer;margin-left: 6px;">删除行</span></span> </div> <div class="tr_normal_opt_c"><span onclick="DocList_AddRow();XFom_RestValidationElements();"><span class="optStyle opt_add_style" title="添加行"></span><span style="position:relative;top:3px;cursor: pointer;margin-left: 6px;">添加行</span></span><span style="margin-left:15px;" onclick="DocList_MoveRowBySelect(-1);"><span class="optStyle opt_up_style" title="上移"></span><span style="position:relative;top:3px;cursor: pointer;margin-left: 6px;">上移</span></span><span style="margin-left:15px;" onclick="DocList_MoveRowBySelect(1);"><span class="optStyle opt_down_style" title="下移"></span><span style="position:relative;top:3px;cursor: pointer;margin-left: 6px;">下移</span></span> </div> <div class="tr_normal_opt_r"> </div> </div> </td> </tr> </tbody> </table> </td> </tr>
08-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值