electron JQuery无法与电子框架一起使用

prdp8dxp  于 2022-12-08  发布在  Electron
关注(0)|答案(2)|浏览(141)

我尝试使用jquery和electron framework一起使用electron fiddle。但是jquery似乎不能正常工作,动画也不能执行。示例如下:https://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_animation1

主文件.js

// Modules to control application life and create native browser window

const {app, BrowserWindow} = require('electron')

function createWindow () {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  })

  // and load the index.html of the app.
  mainWindow.loadFile('index.html')

  // Open the DevTools.
  // mainWindow.webContents.openDevTools()
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', function () {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', function () {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow()
  }
})

索引.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
    <link rel="stylesheet" type="text/css" href="./styles.css">
<script>window.$ = window.jQuery = require('https://code.jquery.com/jquery-3.3.1.slim.min.js');</script>

<script> 
$(document).ready(function(){
  $("button").click(function(){
    $("div").animate({left: '250px'});
  });
});
</script> 

  </head>
  <body>
    
<button>Start Animation</button>

<div style="background:#98bf21;height:100px;width:100px;position:absolute;"></div>

<script>
  // You can also require other files to run in this process
  require('./renderer.js')
</script>

  </body>
</html>

单击按钮后,没有任何React。

brc7rcf0

brc7rcf01#

通常你会通过npm install jquery将electron安装到你的node_modules文件夹中,并要求它在你的renderer.js中。

window.$ = window.jQuery = require('jquery');

属于renderer.js内部。通过这种方式,您可以在本地拥有它,并且yiu不再依赖于外部CDN。

zkure5ic

zkure5ic2#

在2022年,我就是这样让它运转起来的。
安装JQuery

npm install jquery --save

使用script标记将jquery添加到index.html文件中。src应该是node_modules文件夹中jquery的路径

<script type="text/javascript" src="../node_modules/jquery/dist/jquery.min.js"></script>

现在,JQuery将在渲染器进程中作为$renderer.js文件)提供

相关问题