什么时候event.target.value不是字符串? [英] When is an event.target.value not a string?

查看:194
本文介绍了什么时候event.target.value不是字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在触发textinputs keyup / keydown事件时,遇到了 value = String(event.target.value ||)的情况。

I came accross value = String(event.target.value || "") when a textinputs keyup/keydown event is fired.

但是我不确定 event.target.value 不是字符串吗?这可能吗?何时以 event.target.value 传递其他东西?

But i'm not sure when the event.target.value is not a string? Is this possible? When is something else passed off as an event.target.value?

推荐答案

如果 event.target 元素不是输入类型元素,则它将没有 value 属性。例如,如果我单击 div ,则 event.target 是不具有值。

If the event.target element is not an input type element, it will not have a value property. For example, if I click a div then event.target is a div which does not have value.

包装 event.target.value ||不需要 String()中的,因为它始终是值(始终是字符串或 undefined )或值为未定义的空字符串。

Wrapping event.target.value || '' in String() is not necessary as it will always be either value (which is always a string or undefined) or the empty string in the case that value is undefined.

请参见< a href = http://jsfiddle.net/5hBCF/>这个小提琴进行演示。

这篇关于什么时候event.target.value不是字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆