如何在 JavaScript 中获取innerHTML(总字符)的长度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9091188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:33:57  来源:igfitidea点击:

How do i get the length of innerHTML (total characters) in JavaScript?

javascripthtmlcharacterinnerhtml

提问by aburman

I'm trying to get the length of the characters that I attain from innerHTMLand it's kind of come down to last resort, but can't seem to find a more efficient way as the data is inside a slider that I'm trying to use to get the lower value and higher value.

我正在尝试获取我从中获得的字符的长度,innerHTML这有点归结为最后的手段,但似乎无法找到更有效的方法,因为数据位于我尝试使用的滑块内以获得较低的值和较高的值。

var lvalue=document.getElementById("lval").innerHTML;

then I'm spiting the string in the spaces:

然后我在空格中吐出字符串:

var larr=lvalue.split(" ");

The innerHTMLvalue is something like this "2413dsk 134dfa134".

innerHTML值类似于“2413dsk 134dfa134”。

And when i use larr[0].length, I get 1 when I need 7. Is there a solution?

当我使用时larr[0].length,当我需要 7 时,我会得到 1。有解决方案吗?

回答by Tristian

I think it would go something like this:

我认为它会是这样的:

var lvalue = document.getElementById("lval").innerHTML;
var larr = lvalue.split(' ');
var len = 0;

// For each iterates over the index of arrays
for(var i in larr) { 
     len += larr[ i ].length // Acummulate the length of all the strings
}

Or alternatively you could count the spaces first and then substract it from the total length.

或者,您可以先计算空格,然后从总长度中减去它。

// Some people argue about extending the native objects
// but in this case I think this method is a natural fit.
String.prototype.count = function( character ) {
   var l = 0;
   // `this` refers to the string itself
   for(var c in this) n = this[ c ] === character ? ++n : n;
   return n;
}

An then use it like so:

然后像这样使用它:

var lvalue = document.getElementById("lval").innerHTML;

// Subtract total length minus the number of spaces
var len = lvalue.length - lvalue.count(' ');

回答by jerjer

This might be caused by a preceding or leading space.

这可能是由前面或前面的空格引起的。

Try trimming the extra spaces :

尝试修剪多余的空格:

var lvalue=document.getElementById("lval").innerHTML.replace(/^\s+/gi,'').replace(/\s+$/gi,'');

var larr=lvalue.split(" ");

回答by Mateus M. C?rtes

If there is someone like me that doesn't want to use JQuery, here is a example in javascript (only)...

如果有像我这样不想使用 JQuery 的人,这里有一个 javascript 示例(仅)...

The files

文件

The html file...

html文件...

<!-- start body -->
<body>
  <!-- start section -->
    <section id="bar"></section>
  <!-- end section -->
</body>
<!-- end body -->

The javascript file...

javascript文件...

/* start - wut element do ya wanna get? */
foo = document.getElementById('bar');
/* end - wut element do ya wanna get? */

/* start function */
function isEmpty(){
  if (foo.innerHTML.length === 0) {
      /* your code here */
  }
}
/* end function */

What I did

我做了什么

In the HTML

在 HTML

Quite simple, I just created a section and assigned an ID on it. This ID will be used to call her in our javascript.

很简单,我只是创建了一个部分并为其分配了一个 ID。这个 ID 将用于在我们的 javascript 中调用她。

In the JavaScript

在 JavaScript 中

In the var fooI I called the section whose I gave an ID. After it I created a function that "do something" if the length of a element is equal zero.

在 var fooI 中,我调用了我给出了 ID 的部分。之后,我创建了一个函数,如果元素的长度为零,则该函数会“执行某些操作”。

Considerations

注意事项

It works, but if you have a space or a line break the code will not consider it as "empty"...

它有效,但是如果您有空格或换行符,则代码不会将其视为“空”...

回答by Jaec

I didn't see any problem with your code. I run a test here: http://desdegdl.com/lval.htmlProbably, you have one or more spaces at the begining of lval's inner HTML.

我没有看到您的代码有任何问题。我在这里运行一个测试:http: //desdegdl.com/lval.html可能,您在lval的内部 HTML的开头有一个或多个空格。