[英]How to check the type of a parameter passed to a function in JavaScript?
I want to make sure that a parameter that is passed to a function is an element and not a number or a string so my program does not crash?我想确保传递给 function 的参数是一个元素,而不是数字或字符串,所以我的程序不会崩溃?
function myFunction(element){
console.log(element.style.color)
}
Perhaps you're looking for HTMLElement :也许您正在寻找HTMLElement :
function myFunction(element) {
if (element instanceof HTMLElement) {
console.log(element.style.color)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.