簡體   English   中英

如何在獨立的dom上使用querySelectorAll?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM