jQuery:Order Inside .contents()

sjngm

私がこれを行う場合:

function getAllTextNodes(root) {
  root = $(root || "body");
  return root.find("*:not(iframe)").contents().filter(function() {
    return this.nodeType === 3 && //Node.TEXT_NODE = 3
      $.trim(this.nodeValue) !== "";
  });
}

getAllTextNodes($.parseHTML("<div><div>a<div>sub</div>b</div></div>"))

結果は、「a」、「b」、「sub」の配列になります。したがって、それらは構造をトラバースし、要素に到達すると、ネストされた要素を続行する前にその要素を完全に処理するように見えます。

これは理にかなっているかもしれませんが(または場合によっては問題ではありません)、DOMツリーに表示されるのとまったく同じ順序で要素を返すロジックが必要なため、私の側でいくつかの問題が発生します。 「a」、「sub」、「b」が返されるのを見て幸せです。

それはjQueryが意図的に構築したものですか?どういうわけか順序を変更できますか?それともこれはバグですか?

ベルギ

それはjQueryが意図的に構築したものですか?それともこれはバグですか?

意図的に行われたとは思いませんが、セレクターAPIやほとんどの変更メソッドでさえDOMの順序で結果が得られることを考えると、バグと見なされる可能性があります。あなたが示していることから、それはcontents単純で実装されているように見えますflatMap(el => el.childNodes)

どういうわけか順序を変更できますか?

はい、内部jQuery.uniqueSort()で使用するjQueryオブジェクトで使用できますNode.compareDocumentPosition

return $.uniqueSort(root.find("*:not(iframe)").contents().filter(function() {
    return this.nodeType === 3 && $.trim(this.nodeValue) !== "";
}));

ただし、jQueryはとにかくテキストノードには適していません。ここでは、次のようなネイティブDOMAPIを使用する方が簡単な場合がありますNodeIterator

const it = document.createNodeIterator(root[0], NodeFilter.SHOW_TEXT, node => node.data.trim() != ""),
      res = [];
for (let node; node = it.nextNode(); )
    res.push(node);
return res;

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Reading contents inside brackets in Java?

分類Dev

Height of content div not increasing with the contents inside dive

分類Dev

String split and preserve the contents inside the quotation " " - Python

分類Dev

jquery: hiding contents of a div

分類Dev

Empty return from JQuery .contents()

分類Dev

How to align contents to center inside a nested component scenario in React

分類Dev

Will the contents inside the Array garbage collected when we do someArray = []

分類Dev

Jquery - Populate DropDown box with contents of array

分類Dev

get docx file contents using javascript/jquery

分類Dev

Dependent Dropdown Contents with jquery-select2

分類Dev

JQUERY AJAX call returning PHP page contents

分類Dev

jquery doesn't append but replaces contents of div

分類Dev

JS / JQUERY iframe.contents()構文

分類Dev

Reverse order of objects inside RLMArray

分類Dev

How to load contents of multiple files in particular order with Javascript/AJAX

分類Dev

jQuery - for inside for loop

分類Dev

Why inside ob_start file_put_contents is nowhere to be found?

分類Dev

I have a .dd file stored inside a zip folder, how do I view its contents?

分類Dev

How disable jquery mobile's style in the entire contents of a div?

分類Dev

How can I refresh the contents of a DIV using JQuery?

分類Dev

manipulating the loaded contents with Jquery. MVC ASP.NET

分類Dev

What is the execution order inside shiny observeEvent()?

分類Dev

function inside for loop (js/jquery)

分類Dev

Showing labels inside the jQuery Progressbar

分類Dev

I am displaying contents of a java array in reverse order of input, but only half the array is being displayed

分類Dev

Jquery selecting child inside prev not working

分類Dev

How to get number inside a div with jquery

分類Dev

How to randomize .class names inside .hasClass() in jQuery

分類Dev

Should functions be inside jQuery's extend, or outside?

Related 関連記事

  1. 1

    Reading contents inside brackets in Java?

  2. 2

    Height of content div not increasing with the contents inside dive

  3. 3

    String split and preserve the contents inside the quotation " " - Python

  4. 4

    jquery: hiding contents of a div

  5. 5

    Empty return from JQuery .contents()

  6. 6

    How to align contents to center inside a nested component scenario in React

  7. 7

    Will the contents inside the Array garbage collected when we do someArray = []

  8. 8

    Jquery - Populate DropDown box with contents of array

  9. 9

    get docx file contents using javascript/jquery

  10. 10

    Dependent Dropdown Contents with jquery-select2

  11. 11

    JQUERY AJAX call returning PHP page contents

  12. 12

    jquery doesn't append but replaces contents of div

  13. 13

    JS / JQUERY iframe.contents()構文

  14. 14

    Reverse order of objects inside RLMArray

  15. 15

    How to load contents of multiple files in particular order with Javascript/AJAX

  16. 16

    jQuery - for inside for loop

  17. 17

    Why inside ob_start file_put_contents is nowhere to be found?

  18. 18

    I have a .dd file stored inside a zip folder, how do I view its contents?

  19. 19

    How disable jquery mobile's style in the entire contents of a div?

  20. 20

    How can I refresh the contents of a DIV using JQuery?

  21. 21

    manipulating the loaded contents with Jquery. MVC ASP.NET

  22. 22

    What is the execution order inside shiny observeEvent()?

  23. 23

    function inside for loop (js/jquery)

  24. 24

    Showing labels inside the jQuery Progressbar

  25. 25

    I am displaying contents of a java array in reverse order of input, but only half the array is being displayed

  26. 26

    Jquery selecting child inside prev not working

  27. 27

    How to get number inside a div with jquery

  28. 28

    How to randomize .class names inside .hasClass() in jQuery

  29. 29

    Should functions be inside jQuery's extend, or outside?

ホットタグ

アーカイブ