jQueryでドキュメントのルート要素を選択する方法は?


以下は、jQueryを使用してドキュメントのルート要素を選択する方法です。

  • $(document)を使用する方法:
$(document).ready(function(){
  var rootElement = $(document).children().first();
  console.log(rootElement);
});
  • $(document.documentElement)を使用する方法:
$(document).ready(function(){
  var rootElement = $(document.documentElement);
  console.log(rootElement);
});

両方の方法は同じ結果を返します。



About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.