gpt4 book ai didi

javascript - 如何计算每行文本中的符号数?

转载 作者:行者123 更新时间:2023-11-28 00:02:29 25 4
gpt4 key购买 nike

我在预标记中有一些具有固定宽度的文本,我需要计算该文本 block 的每一行中有多少个字符。例如,在下面的示例中,第一行有 80 个字符,第二行有 79 个字符,第三行有 81 个字符,依此类推。当我们有一个带有换行符的空字符串时,它应该只是 1。

pre {
display: block;
white-space: pre-wrap;
background: white;
border-color: white;
word-break: normal;
width: 600px;
}
<pre>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque suscipit, nisl eget dapibus condimentum, ipsum felis condimentum nisi, eget luctus est tortor vitae nunc. Nam ornare dictum augue, non bibendum sapien pulvinar ut. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Cras congue congue purus, quis imperdiet tellus ornare in. Nulla facilisi. Nulla elementum posuere odio ut ultricies. Nullam tempus tincidunt elit eget posuere. Pellentesque sit amet tellus sapien. Praesent sed iaculis turpis. Nam quis nibh diam, sed mattis orci. Nullam ornare adipiscing congue. In est orci, consectetur in feugiat non, consequat vitae dui. Mauris varius dui a dolor convallis iaculis.</pre>

这可能吗?

最佳答案

遵循 How can I count text lines inside an DOM element? Can I? 中找到的方法我终于成功实现了下面的代码。棘手的部分是必须用 css 给出 line-height,这样我们才能计算行数。

[更新]

代码已更新,以便它可以在 Firefox 上运行

<html>
<head>
<style>
pre {
display: block;
white-space: pre-wrap;
background: white;
border-color: white;
word-break: normal;
width: 600px;
line-height: 1.14em; /* must be defined to work */
}
</style>
</head>
<body>
<pre id="preContent">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque suscipit, nisl eget dapibus condimentum, ipsum felis condimentum nisi, eget luctus est tortor vitae nunc. Nam ornare dictum augue, non bibendum sapien pulvinar ut. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Cras congue congue purus, quis imperdiet tellus ornare in. Nulla facilisi. Nulla elementum posuere odio ut ultricies. Nullam tempus tincidunt elit eget posuere. Pellentesque sit amet tellus sapien. Praesent sed iaculis turpis. Nam quis nibh diam, sed mattis orci. Nullam ornare adipiscing congue. In est orci, consectetur in feugiat non, consequat vitae dui. Mauris varius dui a dolor convallis iaculis.</pre>
<script>
function calcLineChars(elem){
var cpy = elem.innerHTML;
var totalLines = countLines(elem);
elem.innerHTML = '';
var result = [];
var charCounter = 0;
var lastCount = 0;
for(var i = 0; i < totalLines; i++){
console.log(i);
elem.innerHTML += cpy[charCounter];
while((i + 1) == countLines(elem) && charCounter < cpy.length - 1){
charCounter ++;
elem.innerHTML += cpy[charCounter];
}
charCounter ++;
var currentLength;
if((i + 1) != countLines(elem)){
currentLength = elem.innerHTML.substring(0, elem.innerHTML.lastIndexOf(' ')).length + 1;
}else{
currentLength = elem.innerHTML.length;
}
result.push(currentLength - lastCount);
lastCount = currentLength;
}
return result;
}

function countLines(elem) {
var elemHeight = elem.offsetHeight;
var lineHeight = elem.style.lineHeight || document.defaultView.getComputedStyle(elem, null).getPropertyValue("line-height");
if(lineHeight.indexOf('px') != -1){
lineHeight = lineHeight.substring(0, lineHeight.length - 2);
}
lineHeight = parseFloat(lineHeight);
var lines = elemHeight / lineHeight;
var intLines = parseInt(lines);
if(lines - intLines >= 0.1){
intLines ++;
}
return intLines;
}

var lineChars = calcLineChars(document.getElementById('preContent'));
var message = '';
for(var i = 0, current; current = lineChars[i]; i++){
message += '\n' + 'line ' + (i + 1) + ' has ' + current + ' chars';
}
alert(message);
</script>
</body>
</html>

关于javascript - 如何计算每行文本中的符号数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31658992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com