javascript - jQuery Before (and other DOM manipulations) Not Executing in Internet Explorer -
i've been searching through stackoverflow, jquery, , google answer, thinking surely there definitive answer on why can't jquery call .before() run in internet explorer. seems work correctly in chrome, firefox, safari , opera.
original problem
have fixed breadcrumb , nav bar @ top of document. rest of document contains many paragraphs enclosed in appropriate <p></p>
tags. before every opening p tag, anchor in format of <a name='paragraphidentifier'>paragraphidentifier</a>
. typically format of pargraph identifier citational (1)(b)(a)
.
since introducing nav bar @ top of page, external hyperlink 1 of these anchors, causes anchor appear beneath toolbar. found seems reasonable fix pixelflips.com found here adding span tags class above anchor.
i used jquery accomplish adding span tags suggests using below jquery code , related css:
if(window.location.hash){ var myhash = window.location.hash; var hashname = myhash.substring(1, myhash.length); $("a[name=" + hashname +"]").before("<span class='anchorfix' id='" + hashname + "'></span>"); // have tried .prepend , .replacewith } .anchorfix{ display: block; height: 12px; /*same height header*/ margin-top: -12px; /*same height header*/ visibility: hidden; }
i have additionally tried below jquery code no success either.
$("a").each(function(index, element) { var aname = $(this).attr('name'); var ahtml = $(this).html(); if(aname) $(this).replacewith("<span class='anchorfix' id='" + aname + "'></span><a name='" + ahtml + "' />" + ahtml);
i have demo using lipsum text on jsfiddle found here. if test in chrome, works. if test in ie, never adds span tag.
update
have updated code above reflect .before() instead of .prepend(). ".before()" tried no success , began trying other functions.
i'm not able post images or add link show screenshot, i'll put in plaintext url hope acceptable demonstrate problem. https://www.dropbox.com/s/8jqf0l93n4tv8kf/screenshot%202015-04-23%2006.32.18.png?dl=0
any guidance or pointers can provide on why executes correctly in chrome not ie (i've tested ie8 , ie11 no success), appreciated. thank you. -eric
i believe you're trying do:
$(document).ready(function (e) { $("a[name]").each(function() { // selects tags name attribute var aname = $(this).attr('name'); // .prepend() adds inside element, .before() adds outside of $(this).before("<span class='anchorfix' id='" + aname + "'></span>"); }); });
Comments
Post a Comment