本文介绍了帮助使用dom创建一个新的链接元素并插入它...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在努力了解如何使用dom,而我正在努力做的事情 是插入一个链接在html中的另一个链接之前,基于它' href值。这不是一个真实世界的例子 - 我只是试图分阶段做这个,以了解发生了什么。我收到一个错误 (对象不支持这个属性或方法)在IE中我不能 弄明白我是什么做错了。谁能告诉我? if(navigator.appName ==" Microsoft Internet Explorer") { var browser =" IE"; } else { var browser =" notIE"; } var e = document.getElementById(''contentWrapper''); if(e) { var a = e.getElementsByTagName(''a''); for(var i = 0; i< a.length; i ++) { if(browser ==" IE") { if(a [i] .getAttribute(' 'href'')!= null&& a [i] .getAttribute(''href'',2).indexOf("://")> = 0& ;& a [i] .getAttribute(''href'',2).indexOf(" bcbsal.org")== -1) { var iconLink = e.createElement(''a''); iconLink.href = a [i] .href; iconLink。 title =''此链接将您带到另一个网站。'; iconLink.appendChild(ec reateTextNode(''Test'')); a [i] .title =''此链接将您带到另一个网站。''; a [i]。 parentNode.insertBefore(iconLink,a [i]); } } else .... 谢谢! Holli 解决方案 var lk = a [i] .href; if(lk&& lk .indexOf(''://'')> 0&& lk.indexOf(''bcbsal.org'')< 0)ment(''''); { var iconLink = document.createElement(''A''); iconLink.href = lk; iconLink.title = ''此链接将您带到另一个网站。''; iconLink.innerHTML =''测试''; / * 或 iconLink.firstChild.nodeValue =''测试''; 或 var tx = document.createTextNode(''Test''); inconLink.appendChild(tx); * / a [i] .title =''此链接将您带到另一个网站。''; a [i] .parentNode.insertBefore(iconLink,a [i]); } } } - Stephane Moriaux et son [moins] vieux Mac 全部放在一起: < div id =" contentWrapper">< br> < a href =" http://www.bcbsal.org"> bcbsal.org< / a>< br> < a href =" http://www.apple.com"> Apple a< / a>< br> < a href ="">清空< / a>< br> < / div> < script type =" text / javascript"> if(document.getElementById){ var wrapper = document.getElementById(''contentWrapper'') ; if(wrapper){ var a = wrapper.getElementsByT agName(''a''); var tmp; var iconLink; var i = a.length; while(i - ){ tmp = a [i]; if(/bcbsal\.org /.test(tmp.href)){ iconLink = document.createElement(''a''); iconLink.href = tmp.href; iconLink.title =''此链接将您带到另一个网站。';; iconLink.appendChild(document.createTextNode(''Test'')); tmp.title =''此链接将您带到另一个网站。'; tmp.parentNode.insertBefore(iconLink,tmp); } } } } < / script> - Rob 小组常见问题:<网址:http://www.jibbering.com/faq/> Hi, I''m trying to understand how to work the dom, and all I''m trying to dois insert a link right before another link in the html based on it''shref value. This isn''t a real world example - I''m just trying to dothis in phases to understand what''s going on. I''m getting an error(Object doesn''t support this property or method) in IE and I can''tfigure out what I''m doing wrong. Can anyone tell me? if (navigator.appName == "Microsoft Internet Explorer"){var browser="IE";}else{var browser="notIE";}var e = document.getElementById(''contentWrapper'');if (e){var a=e.getElementsByTagName(''a'');for (var i=0;i<a.length;i++){if (browser == "IE"){if (a[i].getAttribute(''href'') != null &&a[i].getAttribute(''href'',2).indexOf("://") >= 0 &&a[i].getAttribute(''href'',2).indexOf("bcbsal.org") == -1){var iconLink = e.createElement(''a'');iconLink.href = a[i].href;iconLink.title = ''This link takes you to another web site.'';iconLink.appendChild(e.createTextNode(''Test''));a[i].title = ''This link takes you to another web site.'';a[i].parentNode.insertBefore(iconLink,a[i]);}}else.... Thanks!Holli 解决方案 var lk = a[i].href;if( lk && lk.indexOf(''://'')>0 && lk.indexOf(''bcbsal.org'')<0)ment(''a'');{var iconLink = document.createElement(''A'');iconLink.href = lk;iconLink.title = ''This link takes you to another web site.'';iconLink.innerHTML = ''Test'';/*oriconLink.firstChild.nodeValue = ''Test'';orvar tx = document.createTextNode(''Test'');inconLink.appendChild(tx);*/a[i].title = ''This link takes you to another web site.'';a[i].parentNode.insertBefore(iconLink,a[i]);}}}--Stephane Moriaux et son [moins] vieux MacPutting it all together: <div id="contentWrapper"><br><a href="http://www.bcbsal.org">bcbsal.org</a><br><a href="http://www.apple.com">Apple a</a><br><a href="">Empty a</a><br></div> <script type="text/javascript"> if (document.getElementById){var wrapper = document.getElementById(''contentWrapper''); if (wrapper){var a = wrapper.getElementsByTagName(''a'');var tmp;var iconLink;var i = a.length; while (i--){tmp = a[i]; if ( /bcbsal\.org/.test(tmp.href) ){iconLink = document.createElement(''a'');iconLink.href = tmp.href;iconLink.title = ''This link takes you to another web site.'';iconLink.appendChild(document.createTextNode(''Test ''));tmp.title = ''This link takes you to another web site.'';tmp.parentNode.insertBefore(iconLink,tmp);}}}} </script> --RobGroup FAQ: <URL:http://www.jibbering.com/faq/> 这篇关于帮助使用dom创建一个新的链接元素并插入它...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-23 02:33