如何获取跨度中文本的高度和宽度而不是整个跨度宽度和高度

时间:2018-04-05 08:22:25

标签: javascript html d3.js

我是HTML,如下所示,我只能获得文字的heightwidth,而不是整个范围

var bounds = d3.select(".nodeText").node().getBoundingClientRect();
console.log(bounds.width); // 300
console.log(bounds.height); // 111
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/2.1.3/d3.min.js"></script>
<div style="display: table; width: 300px; height: 111.717px;">
  <span class="nodeText" style="display: table-cell; vertical-align: middle; text-overflow: ellipsis; font-family: Arial; font-size: 10px; font-style: normal; font-weight: normal; color: black; opacity: 1;">Exact 765 (55.23%)</span>
</div>

1 个答案:

答案 0 :(得分:1)

您可以使用for (DateTime date = dateTimePicker.Value.Date; date <= dateTimePicker.Value.AddYears(2); date = date.AddMonths(1)) { ... } 功能。

&#13;
&#13;
createRange
&#13;
var span = d3.select(".nodeText").node();
var textNode = span.childNodes[0];

var range = document.createRange();
range.selectNodeContents(textNode);

var rect = range.getBoundingClientRect();
var height = rect.bottom - rect.top;
var width = rect.right - rect.left;

console.log("height = "+height,"width = "+width);
&#13;
&#13;
&#13;