简体   繁体   English

如何获取事件的textContent长度

[英]How to get the textContent length of an event

I would like to retrieve the number of elements in an event. 我想检索事件中的元素数量。

I use the following function : 我使用以下功能:

      element.bind('keyup', function (event) {
        console.log(event);
        });

The console.log (event) resitute me an object like this one (object shortened for the rendering) console.log(事件)重新设置了一个像这样的对象(缩短了渲染的对象)

n.Event {originalEvent: KeyboardEvent, type: "keyup", isDefaultPrevented: 
 ƒ, timeStamp: 13561.044999980368, jQuery2140638708200199875: true, …}
 altKey: false
 bubbles: true
 cancelable: true
 char: undefined
 charCode: 0
 ...
 scrollTop: 0
 scrollWidth: 1035
 shadowRoot: null
 slot: ""
 spellcheck: true
 style: CSSStyleDeclaration {alignContent: "", alignItems: "", alignSelf: "", 
 alignmentBaseline: "", all: "", …}
 tabIndex: 0
 tagName: "DIV"
 textContent: "abc"
 title: ""
 translate: true
 ...

In this object I would like to retrieve in variable the value of the length of the textContent 在这个对象中,我想在变量中检索textContent的长度值

  textContent: "abc"

Here is a example : 这是一个例子:

https://codepen.io/gregand/pen/PoYevYQ?editors=1111 https://codepen.io/gregand/pen/PoYevYQ?editors=1111

How can I recover this value ? 如何恢复该值?

To achieve expected result, use below code 为了获得预期的结果,请使用以下代码

console.log(event.target.childNodes[0].length)

working code for reference - https://codepen.io/nagasai/pen/JjPvJoL?editors=1111 工作代码以供参考-https://codepen.io/nagasai/pen/JjPvJoL?editors=1111

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM