我们在写代码的时候可能会遇到类似的报错:
Uncaught TypeError: div1.setAttribute is not a function
源代码是这样的:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>设置属性值</title><style>.red {width: 100px;height: 100px;background-color: red;}#yellow {width: 100px;height: 100px;background-color: yellow;}#blue {width: 100px;height: 100px;background-color: blue;}</style></head>
<body><div name="one"></div><div name="two" id="yellow"></div><script>// 获取 name属性为one的 divvar div1 = document.getElementsByName("one");// 给获取的div添加属性,由于在style里面设置了属性,所以该div会有宽高,会变成红色// 原来 name为one的div没有class属性,通过setAttribute(),给他设置了一个属性并赋值 // 相当于 <div name="one" class="red"></div>div1.setAttribute("class","red");</script>
</body>
</html>

报错的一行是

div1.setAttribute("class","red");

报错:Uncaught TypeError: div1.setAttribute is not a function
翻译:未捕获TypeError: div1。setAttribute不是一个函数
报错信息说setAttribute不是一个函数,可是setAttribute没有拼写错,setAttribute的参数都带引号了,为什么还是报错呢?
经过小编的一番努力,发现获取节点可能有误,于是写成了

var div1 = document.getElementsByName("one")[0];

这样写就对了

改正后的完整代码:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>设置属性值</title><style>.red {width: 100px;height: 100px;background-color: red;}#yellow {width: 100px;height: 100px;background-color: yellow;}#blue {width: 100px;height: 100px;background-color: blue;}</style></head>
<body><div name="one"></div><div name="two" id="yellow"></div><script>// 获取 name属性为one的 divvar div1 = document.getElementsByName("one")[0];// 给获取的div添加属性,由于在style里面设置了属性,所以该div会有宽高,会变成红色// 原来 name为one的div没有class属性,通过setAttribute(),给他设置了一个属性并赋值 // 相当于 <div name="one" class="red"></div>div1.setAttribute("class","red");</script>
</body>
</html>

报错原因:
由于获取该元素的时候用的是getElementsByName(),得到的是数组,不是数组里面的元素,而我们要的不是数组,要的是元素。

总结:
当遇到 类似 获取的元素节点.操作元素节点的方法 is not a function 的错误的时候,大概是以下几个原因:

1. 操作节点的方法使用有误

  • 方法名拼错了
  • 方法的参数传错了,可能是没有加引号

2. 获取节点有误

  • 获取元素节点的方法拼写上有错误
  • 如果获取节点返回的是一个数组,你可能会忘了要通过该数组的索引值获取到你想要的元素

报错:Uncaught TypeError: 获取的元素节点.setAttribute is not a function相关推荐

  1. JsonMap对象的获取与前台浏览器报错Uncaught TypeError: Cannot read property ‘map‘ of undefined

    JsonMap对象的获取与前台浏览器报错Uncaught TypeError: Cannot read property 'map' of undefined 经过不断排查以后发现是自己的后台传的值出 ...

  2. js代码中for循环里绑定onclick事件报错Uncaught TypeError Cannot set properties of undefined (setting ‘className‘)

    jsfor循环里绑定onclick事件报错Uncaught TypeError Cannot set properties of undefined (setting 'className') 我想要 ...

  3. 小程序组件报错Uncaught TypeError: Cannot read property 'name' of undefined

    小程序组件报错Uncaught TypeError: Cannot read property 'name' of undefined Component({/*** 组件的属性列表*/propert ...

  4. 【解决】控制台报错Uncaught TypeError: Object(...) is not a function at eval (vue-router.esm-bundler.js

    打开http://localhost:8080/,控制台报错 Uncaught TypeError: Object(...) is not a functionat eval (vue-router. ...

  5. layui报错Uncaught TypeError: tree is not a function

    layui报错Uncaught TypeError: tree is not a function tree({ elem: "#classtree" , data: data , ...

  6. 立即执行函数报错Uncaught TypeError: (intermediate value) is not a function.

    立即执行函数报错Uncaught TypeError: (intermediate value) is not a function. 文章目录 立即执行函数报错Uncaught TypeError: ...

  7. JS报错-Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on...

    报错信息:Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on stric ...

  8. vue-cli 外部引入vue-router报错 Uncaught TypeError: Cannot redefine property: $router

    在优化基于vue-cli开发的vue项目中,把vue-router改成外部引入的时候, /build/webpack.base.conf.js配置参数: externals: {'vue': 'Vue ...

  9. Javascript 报错 Uncaught TypeError: x is not a function 常见错误总结

    这是我近期犯的比较傻的一个错误,一定要mark一下. 以下代码段是借用父类构造函数来实现继承的一种方法, 不过在ES6已经可以使用类了,这个作为了解对阅读以前的代码还是有好处的. 代码非常简单, 1 ...

最新文章

  1. HBase的安装与使用
  2. MySQL - 无索引行锁升级为表锁
  3. 超级简单的配置虚拟机网络yum源
  4. ORB-SLAM学习--GitHub上较好的版本
  5. SystemInit时钟系统初始化函数剖析
  6. 想要玩转实现负载均衡,你知道这些吗?
  7. python如何设计系统界面教程_python中pycharm软件界面介绍与配置
  8. 放弃Venn-Upset-花瓣图,在线拥抱二分网络
  9. python找出主力合约价格_什么是期货主力合约?如何判断主力合约是哪个?
  10. ASP和IIS各一点
  11. [LeetCode] Rotate Array
  12. ubuntu五笔输入法安装_打造最强Windows 10微软拼音输入法 + 600万词库下载
  13. 计算机窗口底色,将电脑的窗口背景调成护眼色-电脑护眼设置
  14. 身份认证之多因素身份认证(MFA)
  15. Android上边抽屉式标题,Android Navigation Drawer样式抽屉的使用
  16. PageHelper:在系统中发现了多个分页插件,请检查系统配置
  17. C语言程序设计实验报告——实验七
  18. thinkphp 6.x 利用 easywechat获取微信公众号粉丝信息
  19. 捷径:通过快捷指令下载应用图标、音乐封面和电影海报
  20. 自动化学报- Teleoperation

热门文章

  1. ssh远程登录Jupyter notebook(七月GPU服务器)
  2. Java知多少(9) import及Java类的搜索路径
  3. [ Problem 1 ] 数独游戏
  4. C# 很基础的那些东西
  5. Bailian4140 方程求解【二分法】
  6. NUC1003 Hangover【数学计算+水题】
  7. POJ3292 UVA11105 Semi-prime H-numbers【筛法打表】
  8. 计算机科学 —— 时间戳(timestamp)
  9. Identity of indiscernibles(不可分与同一性)
  10. jieba(结巴)—— Python 中文分词