DOMException: Failed to execute 'insertBefore' on 'Node'
来源:互联网 发布:网络工程施工与验收 编辑:程序博客网 时间:2024/05/16 13:51
问题:
使用vue开发时,抛出如下异常:
DOMException: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node.
经资料查询发现,导致上述异常是因为在html中使用了v-if来控制局部显隐导致的。
解释:
在vue和大部分的mvvm的前端js框架中,基本都提供了类似 *-if 和 *-show的局部显隐控制指令,而这两个指令在实际运用中是有区别的。
相同点:都可以实现局部html(或者说是dom元素)的显示和隐藏
不同点:显示和隐藏的原理不同。对于*-show指令而言,当为false时不显示控制的dom元素,其实就是display = none,只是将控制的dom元素给隐藏了,打开控制台调试工具,仍然能够看到被隐藏的代码。但是,对于 *-if指令而言,当为false时不显示被控制的dom元素是因为被控制的dom元素被从dom中移除了,也就是说,打开调试工具后,你看不到被隐藏的代码。
v-if代码:
当为false时,显示如下:
v-show代码
当为false时,显示如下:
解决方案
根据上面的解释我们可以得出结论:在v-if为false时,如果我们后续的js操作了v-if控制的dom元素,将会因为该部分dom元素不存在而抛出上述异常。
针对这个问题,我们可以用v-show来代替v-if,就可以解决这个问题。
阅读全文
0 0
- DOMException: Failed to execute 'insertBefore' on 'Node'
- Uncaught DOMException: Failed to execute 'insertBefore' on 'Node': The node before which the new nod
- Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node 解决办法
- Uncaught TypeError: Failed to execute 'removeChild' on 'Node': 1 argument required, but only 0 prese
- Ext submit时报错 Failed to execute 'appendChild' on 'Node': The new child element is null.
- Failed to execute 'texImage2D' on 'WebGLRenderingContext
- maven failed to execute goal on project
- Failed to execute goal on project...
- Failed to execute 'importScripts' on 'WorkerGlobalScope'
- Failed to execute mysql_file_stat on file
- Uncaught SecurityError: Failed to execute 'replaceState' on 'History':解决方案
- Failed to execute aapt
- Uncaught NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file:///E:/test
- Node.insertBefore
- Failed to execute WebDAV PROPPATCH
- Uncaught DOMException: Failed to read the 'contentDocument' property from 'HTMLIFrameElement' 的解决办法
- AJAX请求在微信环境报错:NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load
- [ERROR] Failed to execute goal on project sypro: Could not resolve dependencies for project sy.demo:
- Unity项目进行git托管
- Java实现几种序列化方式总结
- JZ2400 linux下使用eop
- ps -ef 输出结果的具体含义
- Armoured Cable vs. Unarmoured Cable: What's The Difference?
- DOMException: Failed to execute 'insertBefore' on 'Node'
- 算法-数组元素循环右移
- 希拉里:美国完全没有做好应对人工智能发展的准备
- 加州的道路下雪了!英伟达用人工智能“造雪”
- CreateInstance(__uuidof(Connection))执行失败的解决办法
- 《寻梦环游记》背后:一出“硅谷”遇上“好莱坞”的好戏
- Kotlin学习(二)
- MySQL5.6压缩包安装所踩的坑
- 解决tomcat 日志????异常