本文介绍了新手:表上的removeChild()什么都不做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 你好, 在index.html页面上我有一个id =" property_fields"的表格。 < table id =" property_fields" ;名称= QUOT; property_fields" border =" 0"> 它包含具有以下ID的行集: var propertyRows = new Array( " region_row"," town_row"," district_row"," address_row"," house_row"," floor_no_row"," rooms_row"," floors_row"," area_row" ,year_built_row, owner_row,standard_row,garrage_row,parking_row,kitchen_row,balcony_row,gas_row, ; media_row","equipment_row"," guard_row", " building_type_row"," picture1_row"," picture2_row"," picture3_row"," property_remarks_row" ); 如果不需要,我想删除行 - 这取决于 estate_type控件的选择: for(var r = 0; r< propertyRows.le NGTH; r ++) { if(visibility [document.announcement.estate_type.selectedIndex] [r] == 0) { var theRow = document.getElementById(''property_fields'')。rows [r + 1]; document.getElementById(''property_fields'')。removeC hild(theRow) ; //这里 问题:没有什么事情发生 } } (有'二维表称为可见性。 问题是removeChilds()什么都不做,没有视觉效果。 我在Linux中使用FireFox。 请帮忙。在此先感谢。 / RAM / Hello,On index.html page I have a table with id="property_fields". <table id="property_fields" name="property_fields" border="0"> It contains set of rows with the following IDs: var propertyRows = new Array("region_row", "town_row", "district_row", "address_row", "house_row", "floor_no_row", "rooms_row", "floors_row", "area_row", "year_built_row","ownership_row", "standard_row", "garrage_row", "parking_row", "kitchen_row", "balcony_row", "gas_row", "media_row", "equipment_row", "guard_row","building_type_row", "picture1_row", "picture2_row", "picture3_row", "property_remarks_row"); I want to remove row if it is not required - it depends on the selection ofestate_type control: for (var r = 0; r < propertyRows.length; r++){if (visibility[document.announcement.estate_type.selectedIndex][r] == 0){var theRow = document.getElementById(''property_fields'').rows[r + 1];document.getElementById(''property_fields'').removeC hild(theRow); // HEREPROBLEM: nothing happends}} (there''s 2-dimensional table called visibility). The problem is that removeChilds() does nothing, there is no visual effect.I use FireFox in Linux.Please help. Thanks in advance./RAM/推荐答案 < snip> <snip> 在HTML文档/ DOM中TR元素不是TABLE元素的子元素 (尽管在XHTML文档/ DOM中它们可能是),而是孩子 (可能暗示)TBODY,THEAD或TFOOT元素,所以你的 - theRow - 不是你要调用的元素的子元素 - removeChild - 因此方法调用无效。一个相对简单的修复方法是调用元素 的 - removeChild - 方法,即 - theRow - 的 - parentNode - 。 IE: - theRow.parentNode.removeChild(theRow); In HTML documents/DOMs TR elements are not children of TABLE elements(though in XHTML documents/DOMs they may be) but rather the childrenof (possibly implied) TBODY, THEAD or TFOOT elements, so your - theRow- is not a child of the element on which you are calling - removeChild- and as a result the method call is non-effective. A relativelysimple fix would be to call the - removeChild - method of the elementthat was the - parentNode - of - theRow -. I.E:- theRow.parentNode.removeChild(theRow); `table''元素没有'name''属性。 `table'' elements don''t have a `name'' attribute. 取决于你如何定义它。错误控制台中有一个例外。 Depends on how you define that. There is an exception in the Error Console. 'tr''元素是'tbody'元素的子元素,而不是'table'' 元素的子元素,并且重复的参考查找是低效的。 var sel = document.forms [" announcement"]。elements [" estate_type"]; var selIdx = sel.selectedIndex; if(selIdx -1) { var table = document.getElementById(''property_fields''); if(table) { var tbody =(table.tBodies || [null])[0]; if(tbody) { for(var r = propertyRows.length; r--;) { if(visibility [selIdx] [r] == 0) { var theRow = tbody.rows [r + 1]; tbody.removeChild(theRow); } } } } } 我认为`r + 1''是因为你希望表头保持不变。如果你想要 put那行成为'thead''元素,其余为exp合法地在'tbody'' 元素(总是在那里),你可以使用`r''。 你应该使用两个或四个取而代之的是缩进空间。 PointedEars - 现实主义:HTML 4.01严格 福音:XHTML 1.0 Strict 疯狂:XHTML 1.1作为应用程序/ xhtml + xml - Bjoern Hoehrmann The `tr'' element is the child of a `tbody'' element, not of the `table''element, and repeated reference lookup is inefficient. var sel = document.forms["announcement"].elements["estate_type"];var selIdx = sel.selectedIndex;if (selIdx -1){var table = document.getElementById(''property_fields'');if (table){var tbody = (table.tBodies || [null])[0];if (tbody){for (var r = propertyRows.length; r--;){if (visibility[selIdx][r] == 0){var theRow = tbody.rows[r + 1];tbody.removeChild(theRow);}}}}} I presume `r + 1'' is because you want the table header to stay in. If youput that row into a `thead'' element and the rest explicitly in the `tbody''element (that is always there), you can use `r'' instead. You should use two or four spaces for indentation instead.PointedEars--realism: HTML 4.01 Strictevangelism: XHTML 1.0 Strictmadness: XHTML 1.1 as application/xhtml+xml-- Bjoern Hoehrmann 为什么这个奇怪的双重方式指定? 孩子只能是孩子的'这个无性DOM世界中的父母, ? 我们可以忘记.removeChild() 如果我们有下面的函数removeElement()? 函数removeElement(x){ x.parentNode.removeChild(x); } ; ============================= 因此: 函数removeElementById(x){ x = document.getElementById(x); x .parentNode.removeChild(x); }; 未经测试!!! - Evertjan。 荷兰。 (请在我的电子邮件地址中将x'变为点数) Why is this spec-ed in this strange double way? A child can only be a child of it''s parent,in this asexual DOM world? Could we forget about .removeChild()if we have the below function removeElement()? function removeElement(x) {x.parentNode.removeChild(x);}; ============================= and consequently: function removeElementById(x) {x = document.getElementById(x);x.parentNode.removeChild(x);}; Not tested!!!--Evertjan.The Netherlands.(Please change the x''es to dots in my emailaddress) 这篇关于新手:表上的removeChild()什么都不做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-17 21:21