【问题标题】:$(object)[0].offsetTop working in Chrome but not in Firefox$(object)[0].offsetTop 在 Chrome 中工作,但在 Firefox 中不工作
【发布时间】:2014-11-15 01:38:48
【问题描述】:

我正在尝试访问此属性:

$(text)[0].offsetTop

它在 Chrome 中运行良好,但在 Firefox 中我得到“未定义”。 有没有办法在所有浏览器中做到这一点?

见:http://jsfiddle.net/qgqr5m6n/3/

$("body").css("margin", "0px");
var svgNS = "http://www.w3.org/2000/svg";

var svgBox = document.createElementNS(svgNS, "svg");
$("body").append(svgBox);  
var text = document.createElementNS(svgNS, "text");
$(svgBox).append(text);

text.innerHTML = "Hello World";

$(text).attr({ "dominant-baseline": "hanging"});
console.log($(text)[0].offsetTop);

$(text).attr({ "x": 0 , "y":  0  });

如果您想知道我需要这个做什么: 我想在矩形的垂直中心对齐可变字体大小的文本。然而,字体大小总是在字母的实际顶部上方包含一个小间隙。我想要的是实际的像素高度。字母上方的这个间隙正是我将“dominant-baseline”设置为“hanging”后“offsetTop”的值。

【问题讨论】:

  • this的可能重复

标签: javascript jquery google-chrome firefox


【解决方案1】:

使用 $(element).offset() 代替 $(element)[0].offsetTop,它返回一个 Top 和 left 的对象。

【讨论】:

  • 谢谢!但是当我尝试 $(text).offsetTop() 时,我得到“Uncaught TypeError: undefined is not a function”...
  • 请考虑对code应用适当的格式。
猜你喜欢
  • 2016-04-18
  • 2017-05-10
  • 2021-08-12
  • 2016-07-19
  • 2017-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多