简体   繁体   English

为什么这个代码不能在IE11,Chrome,Firefox中运行,但是可以在IE8中运行?

[英]Why won't this code work in IE11, Chrome, Firefox, but will work in IE8?

My developer at work tossed me a small template so I could edit it and make a mockup for a work thing. 我的开发人员在工作中给我一个小模板,所以我可以编辑它并制作一个工作模型。 When he created it, it worked just fine; 当他创造它时,它工作得很好; he made it in IE8. 他在IE8中做到了。 When he emailed it to me and I opened it in IE11/Chrome/Firefox no go. 当他通过电子邮件发送给我时,我在IE11 / Chrome / Firefox中打开它没有去。 Any assistance? 任何帮助?

 <!DOCTYPE html> <html> <head> <script> function handleVisibility(box){ var visible = (box.checked) ? "block": "none"; box.parentElement.nextSibling.style.display = visible; } </script> </head> <body> <div id="divTransfer"> <input id="cbTransferRefer" type="checkbox" value="TransferRefer" onClick = "handleVisibility(this)">Transfer/Refer<br> </div> <div id="pTransferTypes" style="display:none" > Transfer Types: <select> <option value="referred">Referred</option> <option value="internalTaskForm">Escalated via Internal Task Form</option> <option value="cci">Escalated via CCI</option> <option value="pdf">Escalated via PDF Form</option> <option value="transferred">Transferred</option> </select> </div> <div id="divTemplate"> <input id="cbTemplateRefer" type="checkbox" value="TemplateRefer" onClick = "handleVisibility(this)">Template/Refer<br> </div> <div id="pTemplateTypes" style="display:none" > Template Types: <select> <option value="referred">Referred</option> <option value="internalTaskForm">Escalated via Internal Task Form</option> <option value="cci">Escalated via CCI</option> <option value="pdf">Escalated via PDF Form</option> <option value="transferred">Transferred</option> </select> </div> </body> </html> 

You don't necessarily want the nextSibling , you want the nextElementSibling . 你不一定想要nextSibling ,你想要nextElementSibling If there is white-space after your parent element, the next sibling will be that white-space. 如果在您的父元素之后有空格,则下一个兄弟将是空格。 As such, you would want to either remove all white-space between the element, and its sibling, or call nextElementSibling to guarantee you don't get a textNode. 因此,您可能希望删除元素及其兄弟之间的所有空格,或者调用nextElementSibling以保证您不会获得textNode。

It's worth noting that getElementSibling is only supported in IE 9 and above. 值得注意的是,仅在IE 9及更高版本中支持getElementSibling But, if you need IE 8 support, you can simply remove the white-space, keeping nextSibling , or leverage a polyfill . 但是,如果您需要IE 8支持,您可以简单地删除空格,保留nextSibling利用polyfill

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM