【问题标题】:Javascript error: Cannot read property 'parentNode' of nullJavascript 错误:无法读取 null 的属性“parentNode”
【发布时间】:2014-03-18 22:35:56
【问题描述】:

我正在使用的javascript:

javascript: c = '{unit}, ({coords}) {player} |{distance}| {return}';
p = ['Scout', 'LC', 'HC', 'Axe', 'Sword', 'Ram', '***Noble***'];

function V() {
    return 1;
}
window.onerror = V;

function Z() {
    d = (window.frames.length > 0) ? window.main.document : document;
    aid = d.getElementById('editInput').parentNode.innerHTML.match(/id\=(\d+)/)[1];

    function J(e) {
        vv = e.match(/\d+\|\d+/g);
        return (vv ? vv[vv.length - 1].match(/((\d+)\|(\d+))/) : null);
    }
    function K(e) {
        f = parseInt(e, 10);
        return (f > 9 ? f : '0' + f);
    }
    function L(g, e) {
        return g.getElementsByTagName(e);
    }
    function N(g) {
        return g.innerHTML;
    }
    function M(g) {
        return N(L(g, 'a')[0]);
    }
    function O() {
        return k.insertRow(E++);
    }
    function W(f) {
        return B.insertCell(f);
    }
    function P(g, e) {
        g.innerHTML = e;
        return g;
    }
    function X(e) {
        C = B.appendChild(d.createElement('th'));
        return P(C, e);
    }
    function Y(f) {
        return K(f / U) + ':' + K(f % (U) / T) + ':' + K(f % T);
    }
    U = 3600;
    T = 60;
    R = 'table';
    S = 'width';
    s = L(document, R);
    for (j = 0; j < s.length; j++) {
        s[j].removeAttribute(S);
        if (s[j].className == 'main') {
            s = L(L(s[j], 'tbody')[0], R);
            break;
        }
    }
    D = 0;
    for (j = 0; j < s.length; j++) {
        s[j].removeAttribute(S);
        if (s[j].className = 'vis') {
            k = s[j];
            if (t = k.rows) {
                D = t.length;
                break;
            }
        }
    }
    for (E = 0; E < D; E++) {
        l = t[E];
        m = (u = l.cells) ? u.length : 0;
        if (m) {
            u[m - 1].colSpan = 5 - m;
            if (N(u[0]) == 'Arrival:') {
                Q = new Date(N(u[1]).replace(/<.*/i, ''));
            } else {
                if (N(u[0]) == 'Arrival in:') {
                    v = N(u[1]).match(/\d+/ig);
                }
            }
            if (E == 1) {
                G = M(u[2]);
            }
            if (E == 2) {
                w = J(M(u[1]));
            }
            if (E == 4) {
                x = J(M(u[1]));
            }
        }
    }
    y = v[0] * U + v[1] * T + v[2] * 1;
    n = w[2] - x[2];
    o = w[3] - x[3];
    F = Math.sqrt(n * n + o * o);
    H = F.toFixed(2);
    E = D - 2;
    s = L(k, 'input');
    i = s[1];
    h = s[0];
    h.size = T;
    B = O();
    P(W(0), 'Distance:').colSpan = 2;
    P(W(1), H + ' Fields').colSpan = 2;
    B = O();
    X('Unit');
    X('Sent');
    X('Duration');
    X('Name to');
    c = c.replace(/\{coords\}/i, w[1]).replace(/\{distance\}/i, H).replace(/\{player\}/i, G);
    for (j in p) {
        z = Math.round([9.00000000, 10.00000000, 11.00000000, 18.0000000015, 22.00000000, 30.00000000, 35.0000000][j] * T * F);
        A = z - y;
        if (A > 0) {
            I = Y(z);
            B = O();
            P(W(0), p[j]);
            P(W(1), A < T && 'just now' || A < U && Math.floor(A / T) + ' mins ago' || Y(A) + ' ago');
            P(W(2), I);
            C = W(3);
            q = C.appendChild(i.cloneNode(1));
            r = C.appendChild(h.cloneNode(1));
            r.id = 'I' + j;
            r.value = c.replace(/\{duration\}/i, I).replace(/\{sent\}/i, new Date(Q.valueOf() - z * 1000).toLocaleString().replace(/.\d{4}/i, '').replace(/(\w{3})\w*/i, '$1')).replace(/\{return\}/i, new Date(Q.valueOf() + z * 1000).toString().replace(/\w+\s*/i, '').replace(/(\d*:\d*:\d*)(.*)/i, '$1')).replace(/\{unit\}/i, p[j]).replace(/\{attack_id\}/i, aid);
            q.onmousedown = new Function('h.value=d.getElementById(\'I' + j + '\').value;');
        }
    }
}
Z();

我收到的错误:

未捕获的类型错误:无法读取 null 的属性“parentNode”

网址如下所示:

game.php?village=2100&amp;id=4348754&amp;type=other&amp;screen=info_command

【问题讨论】:

  • #editInput-元素可能不在页面上

标签: javascript


【解决方案1】:

有两种可能:

  1. editInput 是一个错字,该元素的实际 id 不同(id 区分大小写)。
  2. 您正在执行此代码,而 DOM 尚未准备好。为防止这种情况,请在 &lt;/body&gt; 结束标记之前执行代码,或将其包装在 windowload 事件或 documentDOMContentLoaded 事件的事件处理程序中。

已编辑如何包装您的代码:

window.onload = function() {
    //your code here
};

【讨论】:

  • 如何将其包装在事件处理程序中?
  • 感谢您编辑 Oscar 帖子,遗憾的是这会将 404 页面重定向到我,或者通常它不会“注入”任何 HTML。我假设 editInput 是一个错字!非常感谢您的帮助。
  • 我知道这条评论迟到了,但编辑后的答案末尾有一个额外的“)”,会引发语法错误。我试图编辑最终答案。
  • @Basper53,你是对的。现在它已经修复了。谢谢
  • 非常感谢亲爱的 :)
【解决方案2】:

在我的情况下,滑块和表单验证器之间存在冲突,因为它们位于不同的页面上,但 javascript 文件会同时针对它们两者,因此所有没有滑块的页面都会出现“无法读取属性 'parentNode ' of null" 错误,所以我在滑块中添加了 If 语句:

if(document.getElementById("mainImage")){
var myImage = document.getElementById("mainImage");
var linkElement = myImage.parentNode;

解决了我的问题,试试吧

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 2022-06-24
    • 2018-05-11
    • 1970-01-01
    相关资源
    最近更新 更多