【问题标题】:How can I define document in electron?如何在电子中定义文档?
【发布时间】:2020-08-15 22:20:41
【问题描述】:

我发现的其他 stackoverflow 问题对我没有帮助,所以我问你。如何在电子中定义 html / javascript 中的文档。出现错误:''文档未定义''。

html代码:

  <div id="container">
      <div id="left_panel"> left content! </div>
      <div id="right_panel">
          <div id="drag"></div> right content!
      </div>
  </div>

javascript:

const {app, BrowserWindow, Menu} = require('electron');
const url = require('url');
Menu.setApplicationMenu(false);
var isResizing = false;
var lastDownX = 0;

function boot() {
  win = new BrowserWindow({
    'minHeight': 300,
    'minWidth': 300
  })
  win.loadURL(url.format({
    pathname: 'index.html',
    slashes: true
  }))
}

app.on('ready', boot);

(function() {
    var container = document.getElementById("container"),
        left = document.getElementById("left_panel"),
        right = document.getElementById("right_panel"),
        handle = document.getElementById("drag");

    handle.onmousedown = function(e) {
        isResizing = true;
        lastDownX = e.clientX;
    };

    document.onmousemove = function(e) {
        // we don't want to do anything if we aren't resizing.
        if (!isResizing) {
            return;
        }

        var offsetRight = container.clientWidth - (e.clientX - container.offsetLeft);

        left.style.right = offsetRight + "px"; 
        right.style.width = offsetRight + "px"; 
    }

    document.onmouseup = function(e) {
        // stop resizing
        isResizing = false;
    }
})();

如果我能得到帮助,那就太好了。 谢谢

【问题讨论】:

    标签: javascript html dom electron


    【解决方案1】:

    executeJavascript 方法或许能帮到你。这允许您访问文档对象。它返回一个 Promise,因此您必须从中提取元素。

    【讨论】:

    • 我添加了代码 "contents.executeJavaScript('fetch("jsonplaceholder.typicode.com/users/1").then(resp => resp.json())', true) .then((result) => { console.log(result ) // 将是来自 fetch 调用的 JSON 对象 })",但它不起作用或我必须做什么
    猜你喜欢
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    • 2017-11-19
    • 2022-06-17
    • 1970-01-01
    • 2020-10-22
    • 2021-11-11
    • 1970-01-01
    相关资源
    最近更新 更多