首页 > 解决方案 > jQuery升级后JS函数坏了

问题描述

我有一个在 SVG 上绘制项目并在文本标签下放置背景的函数。在急需升级到最新的 jQuery 后半部分函数(node.insert...)后,抛出错误:NS_ERROR_FAILURE

function highlighted_label(node, txt, padding)
{
    if( !padding )
        padding = { horizontal: 3, vertical: 2 };

    node.append("svg:text")
        .attr("dominant-baseline", "middle")
        // IE 9 doesn't support dominant-baseline, so shift the text manually
        // Opera dosen't seem to support even y/dy ...
        .attr("y", $ && $.browser.msie && parseInt($.browser.version, 10) < 10 ? 4 : 0)
        .attr("text-anchor", "start")
        .text(typeof txt === "function" ? function(d, i) { return txt.call(this, d, i) } : txt)
    ;

    // we need to add highlight background after text was appended so we can get text dimensions,
    // but we also need to insert the background before text so it will be drawn under the text
    node.insert("svg:rect", "text")
        .attr("width", function(d) { return this.nextSibling.getBBox().width + 2 * padding.horizontal; })
        .attr("height", function(d) { return this.nextSibling.getBBox().height + 2 * padding.vertical; })
        .attr("x", function(d) { return this.nextSibling.getBBox().x - padding.horizontal; })
        .attr("y", function(d) { return this.nextSibling.getBBox().y - padding.vertical; })
        .attr("rx", 2)
    ;
}

如果我用数值替换宽度、高度、x 和 y,它可以工作,但显然我需要动态生成这些值。我尝试将 nextSibling 更改为 nextNode 和 nextElementSibling 但无济于事。

我错过了什么?

标签: javascript

解决方案


推荐阅读