从v10开始,jsdom提供了更灵活的API。

https://swashata.me/blog/use-jquery-jsdom-v11/

  const testHTML = `
  <!DOCTYPE html>
  <html>
  <head>
  </head>
  <body>
    <input type="text" id="fiptest">
  </body>
  </html>
  `;
  const { JSDOM } = require( 'jsdom' );
  const jsdom = new JSDOM( testHTML );

  // Set window and document from jsdom
  const { window } = jsdom;
  const { document } = window;
  // Also set global window and document before requiring jQuery
  global.window = window;
  global.document = document;

  const $ = global.jQuery = require( 'jquery' );

  console.log( `jQuery ${jQuery.fn.jquery} working! Yay!!!` );
  const inputElement = $( '#fiptest' );
  console.log( inputElement.length );

相关文章:

  • 2022-12-23
  • 2021-04-12
  • 2022-12-23
  • 2022-01-07
  • 2021-08-14
  • 2022-03-01
  • 2021-10-31
  • 2021-12-05
猜你喜欢
  • 2021-10-25
  • 2021-09-11
  • 2021-11-09
  • 2022-12-23
  • 2021-12-12
  • 2021-12-20
  • 2021-12-23
相关资源
相似解决方案