Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

javascript - parentNode or previousElementSibling not working in IE8

I have some javascript, and either parentNode or previviousElementSibling seems to be breaking in IE8. The code works fine in firefox and IE9. This is the line that's not getting implemented:

$(submitter.parentNode.parentNode.previousElementSibling).children('#mark_as_broken').show();

code is something like

<form><div><input id=mark_as_broken></input></div></form>
<form><div><input id=mark_as_fixed></input></div></form>

where the mark_as_fixed input is the submitter. This works on other browsers.

any ideas which bit of it won't work, and why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

previousElementSibling is not supported until IE9.

http://www.quirksmode.org/dom/w3c_core.html#t84

Here's a function that should work. Haven't tested it yet. Seems to work.

var previousElementSibling = function( el ) {
    if( el.previousElementSibling ) {
        return el.previousElementSibling;
    } else {
        while( el = el.previousSibling ) {
            if( el.nodeType === 1 ) return el;
        }
    }
}

$( previousElementSibling(submitter.parentNode.parentNode) )

EDIT:

You didn't mention jQuery, but you appear to be using its API. If so, you can just do this:

$(submitter).closest('form').prev().find('#mark_as_broken').show();

Based on your markup, it appears as though you should be using .find() instead of .children().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...