简体   繁体   English

使用JavaScript按钮打开对话框

[英]Open dialog with JavaScript button

I found the following code which shows the "open file dialog". 我发现以下代码显示了“打开文件对话框”。

var fileSelector = document.createElement('input');

fileSelector.setAttribute('type', 'file');

var selectDialogueLink = document.createElement('a');

selectDialogueLink.setAttribute('href', '');
selectDialogueLink.innerText = "Select File";
selectDialogueLink.onclick = function () {
    fileSelector.click();
    return false;
}

document.body.appendChild(selectDialogueLink);

How can I have this function on an existing button called PSD["Buttonname"]? 如何在名为PSD [“ Buttonname”]的现有按钮上使用此功能? And how do I get the name of the selected file in a string? 以及如何以字符串形式获取所选文件的名称? I would love to have this in plain JavaScript. 我很想在普通的JavaScript中使用它。

Could anybody please push me in the right direction? 有人能把我推向正确的方向吗?

innerText is something you used to use in the dark ages of the browser wars. innerText是您在浏览器大战的黑暗时代曾经使用的东西。 These days you use textContent = "text"; 这些天,您使用textContent =“ text”; For adding an event to any DOM node you use 要将事件添加到任何DOM节点,请使用

fileSelector.addEventListener( "click", function(){ alert( "here be dragons" ); });

Maybe your button has an id so you could use 也许您的按钮有一个ID,所以您可以使用

document.getElementById( "someID" ).addEventListener( "click", function(){ alert( "here be dragons" ); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM