parentNode或previousElementSibling在IE8中不起作用 [英] parentNode or previousElementSibling not working in IE8

查看:790
本文介绍了parentNode或previousElementSibling在IE8中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些javascript,并且在IE8中,parentNode或previviousElementSibling似乎都在破坏。该代码在firefox和IE9中运行良好。这是未实现的行:

  $(submitter.parentNode.parentNode.previousElementSibling).children('#mark_as_broken' )。显示(); 

代码类似于

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

其中mark_as_fixed输入是提交者。这适用于其他浏览器。



任何想法都不起作用,为什么?

previousElementSibling



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



这是一个应该有效的功能。 尚未测试过。似乎工作。

  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))






编辑:



你没有提到jQuery,但你似乎在使用它的API。如果是这样,你可以这样做:

  $(提交者).closest('form')。prev()。find ( '#mark_as_broken')显示(); 

根据您的标记,您似乎应该使用 .find ()而不是 .children()


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?

解决方案

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().

这篇关于parentNode或previousElementSibling在IE8中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆