javascript - 如何检查传递给 JavaScript 函数的参数类型?
问题描述
我想确保传递给函数的参数是元素而不是数字或字符串,这样我的程序就不会崩溃?
function myFunction(element){
console.log(element.style.color)
}
解决方案
也许您正在寻找HTMLElement:
function myFunction(element) {
if (element instanceof HTMLElement) {
console.log(element.style.color)
}
}
推荐阅读
- c++ - 使用不同的 enable_if 条件选择方法
- azure-ad-b2c - MSAL 请求 HTTP 页面并引发“混合内容”错误
- python - 抓取新的请求 URL
- javascript - 通过按钮选择 ag-grid-table 中的下一行(如果下一行在下一页上)?
- c++ - 在 C++ 中,std::atomic 生成竞争条件。但是当使用 std::async 时,它会产生竞争条件
- javascript - html 中的 Srcset 混淆
- swift - 如何在 SwiftUI 中创建蓝视图?
- android - 尝试使用 android.support 为 android 添加 MSAL
- vue.js - 无法在其他组件中使用文件上传组件
- windows - 批处理脚本错误:获取一年中的哪一天