javascript - parentNode 或 previousElementSibling 在 IE8 中不工作

标签 javascript internet-explorer-8

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

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

代码是这样的

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

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

有什么想法是行不通的,为什么?

最佳答案

previousElementSibling 直到 IE9 才被支持。

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。如果是这样,您可以这样做:

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

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

关于javascript - parentNode 或 previousElementSibling 在 IE8 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5197825/

相关文章:

javascript - 选择没有id的html对象

google-maps - 使用 Google HeatMap 覆盖时,IE8 会抛出长时间运行的脚本错误

css - IE8 的背景剪辑修复

css - 仅在 IE8 中出现奇怪的渲染问题

windows-7 - 在 IE8 中使用 window.location 时出现 Microsoft JScript 运行时错误

javascript - 在背景图像 svg 中使用 JavaScript

javascript - 如何从导航栏滚动到 ID

javascript - 为什么我们需要 isPrototypeOf 呢?

javascript - 简单电话 RegXP 返回 : Uncaught SyntaxError: Invalid or unexpected token

javascript - IE8 : How to set focus to a popup and back without blinking orange