用什么代替“上下文”来修改SVG?

时间:2019-06-21 10:07:59

标签: jquery html svg

我正在尝试更新一个用jQuery 1编写的,以 SVG 处理的现有项目,以与当前版本一起运行。它仍然使用.context属性,现在当然已弃用该属性并引发错误。 我已经尝试过任何方法,但是不知道如何到达对象并更改文本。

我发现在处理HTML文档时,通常可以使用“文档”而不是.context。但是在这种情况下,我试图访问SVG元素内的文本标签。 使用旧的jQuery版本,我发现目标对象是以下类型的[object SVGTextElement]

    labelMeasure = qSVG.create("none", "text", {
      x: 0,
      y: -10,
      'font-size': '1.2em',
      stroke: "#ffffff",
      "stroke-width": "0.4px",
      'text-anchor': 'middle',
      fill: "#3672d9"
    });

    binder.graph.append(labelMeasure);
    $('#boxbind').append(binder.graph);
  } else {
    cross.attr({
      "transform": "translate(" + (snap.x) + "," + (snap.y) + ")"
    });

    if (action == 1) {
      var startText = qSVG.middle(pox, poy, x, y);
      var angleText = qSVG.angle(pox, poy, x, y);
      var valueText = qSVG.measure({
        x: pox,
        y: poy
      }, {
        x: x,
        y: y
      });
      binder.size = valueText;
      binder.x = startText.x;
      binder.y = startText.y;
      binder.angle = angleText.deg;
      valueText = (valueText / meter).toFixed(2) + ' m';
      labelMeasure.context.textContent = valueText; // <--
      binder.update();
    }
  }
}

这是使用不赞成使用的.context属性时的错误消息:

  

TypeError:未定义不是对象:(正在评估   'labelMeasure.context.textContent = valueText')

1 个答案:

答案 0 :(得分:0)

很难确定,因为我不知道qSVG()的实际作用。

但是您可以尝试更换

labelMeasure.context.textContent = valueText;

使用

labelMeasure.text(valueText);

如果这不起作用,则应使用minimal, reproducible example更新问题。

https://jsfiddle.net/wavmxpzq/