Javascript “溢出:隐藏”的替代选项,该选项不剪裁中间字符

Javascript “溢出:隐藏”的替代选项,该选项不剪裁中间字符,javascript,css,overflow,hidden,clipping,Javascript,Css,Overflow,Hidden,Clipping,我正在使用overflow:hidden属性来限制计算器显示中显示的数字。但是,overflow:hidden会在字符中间剪辑字符,这是我想要避免的 建议使用文本溢出:“”,它与浏览器不太兼容。关于如何限制字符数以及在字符之间而不是中间字符之间剪辑,您还有其他建议吗。(我不想在显示器上看到半个数字) 鼓励使用JavaScript解决方案 这是我目前的解决方案。它只在输入数字时有效,而在输入运算符时无效 让digitsArray=[]; 让digitsString=“”; 让displayOut

我正在使用
overflow:hidden
属性来限制计算器显示中显示的数字。但是,
overflow:hidden
会在字符中间剪辑字符,这是我想要避免的

建议使用
文本溢出:“”,它与浏览器不太兼容。关于如何限制字符数以及在字符之间而不是中间字符之间剪辑,您还有其他建议吗。(我不想在显示器上看到半个数字)

鼓励使用JavaScript解决方案

这是我目前的解决方案。它只在输入数字时有效,而在输入运算符时无效

让digitsArray=[];
让digitsString=“”;
让displayOutput=“”;
让按下的按钮排列=[];
让按下按钮字符串=”;
让visibleArray=[];
让visibleText=“”;
让结果=”;
让显示文本宽度;
让数个字符;
让像素成为字符;
让数个可见字符;
让外层宽度;
window.onload=函数(){
outerDivWidth=document.getElementById(“outerDiv”).offsetWidth;
};
window.onresize=函数(){
outerDivWidth=document.getElementById(“outerDiv”).offsetWidth;
};
document.onkeypress=函数(事件){
让buttonValue=event.which | | event.keyCode;
让buttonCharacter=String.fromCharCode(buttonValue);
如果(/[0123456789]/.测试(按钮字符)){
单击数字按钮(按钮字符)
}
如果(按钮字符==“+”){
单击操作按钮(“+”、“+”);
}
如果(按钮字符==“-”){
单击操作按钮('-','-');
}
如果(按钮字符==“*”){
单击操作按钮(“*”、“×”)
}
如果(按钮字符==“/”){
单击操作按钮(“/”、“÷”);
};
如果(按钮字符==“=”){
等同于();
}
};
函数show(){
}
函数scaleFont(){
//放在这里,这样每次按下按钮都会执行//
console.log(“offsetwidth:+document.getElementById(“displayText”).offsetwidth)
displayTextWidth=document.getElementById(“displayText”).offsetWidth;
log(“displayTextWidth:+displayTextWidth”);
numberOfCharacters=visibleText.length;
日志(“numberOfCharacters:+numberOfCharacters”);
pixelsPerCharacter=displayTextWidth/numberOfCharacters;
log(“pixelsPerCharacter:+pixelsPerCharacter”);
numberOfVisibleCharacters=数学地板(外径/像素字符);
log(“numberOfVisibleCharacters:+numberOfVisibleCharacters”);
displayText=visibleText.slice(-numberOfVisibleCharacters)
document.getElementById(“displayText”).innerHTML=displayText;
console.log(“offsetwidth:+document.getElementById(“displayText”).offsetwidth)
}
功能单击数字按钮(按钮){
//等式发生了//
如果(按ButtonSarray[按ButtonSarray.length-1]==“=”)){
按下按钮阵列=[];
按按钮排列。按(按钮);
pressedButtonsString=pressedButtonsArray.join(“”);
可视阵列=[];
可视阵列。按下(按钮);
visibleText=visibleArray.join(“”);
document.getElementById(“displayText”).innerHTML=visibleText;
}
//等式没有发生//
否则{
按按钮排列。按(按钮);
pressedButtonsString=pressedButtonsArray.join(“”);
可视阵列。按下(按钮);
visibleText=visibleArray.join(“”);
document.getElementById(“displayText”).innerHTML=visibleText;
}
scaleFont();
控制台日志(“PBA:+按下按钮阵列);
控制台日志(“VA:+visibleArray”);
console.log(“PBS:+按下按钮字符串”);
console.log(“VT:+visibleText”);
}
功能单击操作按钮(按钮,符号){
如果(按ButtonsArray.length>0&(/[0123456789=]/.test(按ButtonsArray[pressedButtonsArray.length-1]))| |(按钮=“-”&&pressedButtonsArray[pressedButtonsArray.length-1]!=“-”){
如果(按ButtonSarray[按ButtonSarray.length-1]==“=”)){
按下按钮阵列=[];
按按钮排列。按(结果,按钮);
pressedButtonsString=pressedButtonsArray.join(“”);
可视阵列=[];
visibleArray.push(结果、符号);
visibleText=visibleArray.join(“”);
document.getElementById(“displayText”).innerHTML=visibleText;
}
否则{
按按钮排列。按(按钮);
pressedButtonsString=pressedButtonsArray.join(“”);
可视阵列推送(符号);
visibleText=visibleArray.join(“”);
document.getElementById(“displayText”).innerHTML=visibleText;
}
}
scaleFont();
控制台日志(“PBA:+按下按钮阵列);
控制台日志(“VA:+visibleArray”);
console.log(“PBS:+按下按钮字符串”);
console.log(“VT:+visibleText”);
}
函数等价(){
如果(按按钮排列长度>0&(/[0123456789]/.test(按按钮排列长度-1])){
按按钮排列。按(“=”);
结果=评估(按按钮字符串);
pressedButtonString=result.toString();
可视阵列。按(“=”);
visibleText=result.toString();
document.getElementById(“displayText”).innerHTML=visibleText;
}
scaleFont();
控制台日志(“PBA:+按下按钮阵列);
控制台日志(“VA:+visibleArray”);
console.log(“PBS:+按下按钮字符串”);
console.log(“VT:+visibleText”);
}
函数del(){
如果(按ButtonSarray[按ButtonSarray.length-1]==“=”)){
按下按钮阵列=[];
按按钮字符串=”;
可视阵列=[];
visibleText=“”;
结果=”;
document.getElementById(“displayText”).innerHTML=“”;
}
否则{
按下按钮排列拼接(-1,1);
pressedButtonsString=pressedButtonsArray.join(“”);
可见阵列拼接(-1,1)
visibleText=visibleArray.join
.number {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}