[英]How to use querySelectorAll on an detached dom?
我需要传递给actionSorter()方法和HTMLCollection,我想知道如何创建与DOM分离的临时div。
有任何想法吗?
var temp = document.createElement('div');
temp.className = 'temp';
temp.dataset.href = 'zoomout';
var coll = document.querySelectorAll('.temp');
this.actionSorter(coll);
actionSorter: function($el) {
var href = $el[0].dataset.href;
if (href === 'viewup') {
Viewer.itemAnimateUp();
}
if (href === 'viewright') {
Viewer.itemAnimateRight();
}
if (href === 'viewdown') {
Viewer.itemAnimateDown()
}
if (href === 'viewleft') {
Viewer.itemAnimateLeft();
}
return false;
},
您不需要执行querySelectorAll
因为您已经有了该元素。
足够通过temp
元素
this.actionSorter(temp);
但是,如果您的元素更复杂,并且您想在未附加到DOM的临时元素中找到某些内容,则可以调用该元素的querySelectorAll
方法,例如
temp.querySelectorAll(".selector")
请注意, querySelectorAll
返回一个元素数组。
更新:根据您的代码...只需将数组作为参数传递
var temp = document.createElement('div');
temp.className = 'temp';
temp.dataset.href = 'zoomout';
this.actionSorter( [temp] ); // <--- note temp into array
尝试这样的事情
var temp = document.createElement('div');
temp.className = 'temp';
temp.dataset.href = 'zoomout';
this.actionSorter(temp);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.