【问题标题】:Can't edit input text field after window.alert()window.alert() 后无法编辑输入文本字段
【发布时间】:2019-11-10 08:30:00
【问题描述】:

我有这个 Electron 应用程序(使用 NodeJS、Bootstrap、AngularJS),其中包含一些可以编辑的文本输入字段。 我有一个触发 window.alert() 的按钮 触发后,文本输入字段不再可编辑。

点击应用程序的其他元素不会改变任何事情。

单击另一个应用程序然后返回该应用程序可以解决问题。 使用 Chrome 检查器单击其中一个文本输入字段也可以修复它。

这些是我的输入字段的一些 CSS 属性

element.style {
}
.form-control:focus {
    color: #495057;
    background-color: #fff;
    border-color: #80bdff;
    outline: 0;
    box-shadow: 0 0 0 0.2rem rgba(0,123,255,.25);
}
.form-control {
    background-color: inherit;
    position: relative;
    z-index: 10;
}
*:focus {
    outline: 0;
}
.form-control {
    display: block;
    width: 100%;
    height: calc(2.25rem + 2px);
    padding: .375rem .75rem;
    font-size: 1rem;
    line-height: 1.5;
    color: #495057;
    background-color: #fff;
    background-clip: padding-box;
    border: 1px solid #ced4da;
    border-radius: .25rem;
    transition: border-color .15s ease-in-out,box-shadow .15s ease-in-out;
}
*, ::after, ::before {
    box-sizing: border-box;
}
input:focus, textarea:focus, select:focus {
    outline-offset: -2px;
}
:focus {
    outline: -webkit-focus-ring-color auto 5px;
}
input {
    padding: 1px 0px;
}
input {
    -webkit-appearance: textfield;
    background-color: white;
    -webkit-rtl-ordering: logical;
    cursor: text;
    padding: 1px;
    border-width: 2px;
    border-style: inset;
    border-color: initial;
    border-image: initial;
}
input, textarea, select, button {
    text-rendering: auto;
    color: initial;
    letter-spacing: normal;
    word-spacing: normal;
    text-transform: none;
    text-indent: 0px;
    text-shadow: none;
    display: inline-block;
    text-align: start;
    margin: 0em;
    font: 400 13.3333px Arial;
}
input, textarea, select, button, meter, progress {
    -webkit-writing-mode: horizontal-tb !important;
}
html {
    font-family: sans-serif;
    line-height: 1.15;
    -webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    -ms-overflow-style: scrollbar;
    -webkit-tap-highlight-color: transparent;
}
*, ::after, ::before {
    box-sizing: border-box;
}
*, ::after, ::before {
    box-sizing: border-box;
}

这是警报的代码。

电子弹出窗口elerem.dialog.showMessageBox(elerem.getCurrentWindow(), options) 没有引起问题,只有window.alert("Your settings list is up to date");

req.on("end", function () {
    res = Buffer.concat(chunks).toString('utf8');
    $scope.checkingForUpdate = false;
    $scope.$apply();
    if (res == fs.readFileSync(CONFIG_FILE_PATH, 'utf8')) {
        window.alert("Your settings list is up to date");
    } else {
        let options = {
            buttons: ["Yes", "No"],
            message: "An updated settings list is available would you like to get it ? (You'll loose all unsaved settings)"
        }
        let response = elerem.dialog.showMessageBox(elerem.getCurrentWindow(), options)
        if (response == 0) {
            $scope.refresh = true;
            config.writeToFile(res, CONFIG_FILE_PATH, function (err) {
                if (err) {
                    window.console.log(err);
                } else {
                    window.alert("Your settings list has been updated with success");
                }
            });
        }
    }
    if ($scope.refresh) {
        config.init();
        $scope.$apply();
    }
});

我希望 message.alert() 不会改变我的文本输入字段的行为。

【问题讨论】:

  • 我看到您将此标记为 JavaScript 问题,但没有列出任何代码 - 警报是否也会修改页面,例如将警报设为模式框?

标签: javascript html css electron


【解决方案1】:

根据this post on StackOverflow alert 不被电子支持,因为它在执行时会冻结线程。虽然您可能可以调用它,但如果可能的话,您可能希望看到有关移动到电子的 dialog 或页面内模态(如 MaterialUI 或 Bootstrap 模态)的信息。

【讨论】:

  • 我会补充一点,confirm 似乎造成了同样的问题。
【解决方案2】:

我有点晚了,但我也遇到了 window.alert() 的问题。我不知道它也不支持,所以我设置了一种方法来发送带有电子对话框的消息,如上所述。以为我会分享以防其他人遇到困难。

我完成这项工作的方法是使用 preload.js 文件。 reZach 在this thread 上有一篇关于 preload.js 和 ipcRenderer 的精彩帖子。

以下是我如何使用它设置我的应用程序警报。

//In preload.js

const {ipcRenderer, contextBridge} = require('electron');

contextBridge.exposeInMainWorld(
    "api" : {
        messageMain: (channel, message) => {
            let validChannels = ['send-alert'];
            if (validChannels.includes(channel)) {
                 ipcRenderer.send(channel, message);
            }
        }
    }
)


//In main.js

//BrowserWindow Setup
let mainWindow;

function createWindow(){
    let win = new BrowserWindow({
        show: false,
        webPreferences: {
            nodeIntegration: false,
            contextIsolation: true,
            enableRemoteModule: false,
            preload: __dirname + '/preload.js'
        }
    })

    //load file/URL here 

    win.once("ready-to-show", () => {
        win.show()
    }

}


//ipcMain listener

ipcMain.on("send-alert", (event, incomingMessage) => {
    const options = {
        type: "none",
        buttons: ["Okay"],
        title: "Alert Message!"
        message: incomingMessage
    }
    dialog.showMessageBox(mainWindow, options)
})

app.on('ready', () => {
    mainWindow = createWindow()
}

//In ipcRenderer

window.api.messageMain("send-alert","Write your alert message here!")

【讨论】:

    【解决方案3】:

    就像上面提到的@Grim1101,使用电子的对话框而不是alert()。例如,如果您想显示带有标题和内容的错误消息,您可以使用dialog.showErrorBox(title, content)

    {dialog} = require('electron').remote
    
    dialog.showErrorBox('title here','type error message here');
    

    【讨论】:

      猜你喜欢
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-14
      • 1970-01-01
      相关资源
      最近更新 更多