|
|
为什么要用定时器呢,原本的function 变化检测(),用MutationObserver来检测变化,改这个不用引入额外的代码
- function 变化检测() {
- const 观察 = new MutationObserver(function (变化标记) {
- 变化标记.forEach(function (变化) {
- 变化.addedNodes.forEach(function (节点) {
- if (节点.nodeType === Node.ELEMENT_NODE) {
- const images = 节点.matches('.myimg img') ? [节点] : 节点.querySelectorAll('.myimg img');
- images.forEach(img => {
- const 替代文本 = img.getAttribute('alt')
- if (Object.prototype.hasOwnProperty.call(放大镜内容映射表, 替代文本)) {
- let showText = 放大镜内容映射表[替代文本];
- if (showImg && showText)
- {
- showText = addImgUrl(showText);
- }
- 添加悬停监听器(img, showText)
- }
- });
- }
- })
- })
- })
- const 目标容器 = document.querySelector('#ct.wodexunzhang')
- if (目标容器) {
- 观察.observe(目标容器, { childList: true, subtree: true })
- }
- }
复制代码 |
评分
-
查看全部评分
thh866
:是大佬,之前都没仔细研究过这一部分,这样确实改动小很多
-
|