如何获取所有行中所有文本的长度。我有变量范围的问题。我只得到最后一行的文本长度。
代码:
pointerclick: function (evt, x, y) {
var className = evt.target.parentNode.getAttribute('class');
if (className === 'element-tool-remove')
{
this.model.remove();
console.log("element removed");
var embeddedCells = parent1.getEmbeddedCells();
var t = new Array();
var maxTextLength;
for(var d = 0; d < embeddedCells.length; d++)
{
t[d] = embeddedCells[d].attributes.attrs.text.text.length;
var maxTextLength = Math.max(t[d]);
//console.log("max text length is: " + maxTextLength);
}
console.log("max text length is: " + maxTextLength); // printing only last element's text length but not the max value out of all
}
joint.dia.CellView.prototype.pointerclick.apply(this, arguments);
}
如何从所有行中获取最大值?
答案 0 :(得分:2)
尝试:
pointerclick: function (evt, x, y) {
var className = evt.target.parentNode.getAttribute('class');
if (className === 'element-tool-remove')
{
this.model.remove();
console.log("element removed");
var embeddedCells = parent1.getEmbeddedCells();
var t = new Array();
var maxTextLength = 0;
for(var d = 0; d < embeddedCells.length; d++)
{
t[d] = embeddedCells[d].attributes.attrs.text.text.length;
maxTextLength = Math.max(t[d], maxTextLength);
//console.log("max text length is: " + maxTextLength);
}
console.log("max text length is: " + maxTextLength); // printing only last element's text length but not the max value out of all
}
joint.dia.CellView.prototype.pointerclick.apply(this, arguments);
}
我已将0指定为maxTextLength变量的默认值,而在循环中,如果新值较大,则替换maxTextLength的值。我没有办法测试该代码,但它应该可以工作。