[英]How to fix text length in a paragraph?
I'm tryin' to create a simple calculator using JS. 我正在尝试使用JS创建一个简单的计算器。 I'm using a paragraph tag as a display and the problem is that the text can go beyond the line.
我正在使用段落标记作为显示,但问题是文本可能会超出一行。
My calculator: 我的计算器:
But when I enter more than like 12 buttons this happens: 但是,当我输入的内容超过12个按钮时,就会发生这种情况:
They way I'm adding numbers looks like: 他们添加数字的方式如下所示:
$('#5').click(function() {
$("#mainline").text(function(i, oldtext) {
return oldtext.replace(/^0$/, '') + '5';
});
});
I tried to put all buttons in a loop that will check the length of the paragraph tag and if it's more than 12 then: 我试图将所有按钮放入一个循环中,该循环将检查段落标记的长度,如果大于12,则:
document.getElementsByTagName("button").disabled = true
But I didn't work. 但是我没工作。 What should I do?
我该怎么办?
HTML: HTML:
<div class='calculator'>
<div class='lines'><p id="mainline">0</p></div>
<div id="row1">
<button id='AC'>AC</button>
<button id='pm'><sup>+</sup>/<sub>-</sub></button>
<button>%</button>
<button id='dvd'>/</button>
</div>
CSS: CSS:
.calculator {
display: inline-block;
position: relative;
padding-left: 37%;
padding-top: 7%;
}
button {
width: 50px;
height: 40px;
margin-bottom: 1px;
}
#mainline {
border: 3px solid #FF9500;
text-align: right;
}
You have a couple things to think of, as I read in comments. 正如我在评论中所读,您需要考虑几件事。
But here is a suggestion that may be of interest: CSS direction:rtl
and text-overflow:ellipsis
... 但是,这里有一个有趣的建议:CSS
direction:rtl
和text-overflow:ellipsis
...
$("#test").on("keyup",function(){ $("#display").text($(this).val()); });
#display{ /* Just for this demo */ height:1em; width:8em; border: 1px solid orange; display:inline-block; margin-top:0.4em; /* suggestion */ direction: rtl; overflow:hidden; text-overflow: ellipsis; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> Type in! <input id="test"><br> Result: <div id="display"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.