简体   繁体   English

Javascript 创建按钮,获取使用 onclick 按下的按钮的 ID(

[英]Javascript created button, get id of the button pressed with onclick(

Note: this is my first project, sorry if this is obvious, I've looked everywhere and can't find it注意:这是我的第一个项目,对不起,如果这很明显,我到处找都找不到

I'm working on a website that would serve as a better UI then file explorer/VLC, so I've made a button where you can upload all your video files.我正在开发一个网站,它可以作为一个比文件浏览器/VLC 更好的用户界面,所以我制作了一个按钮,您可以在其中上传所有视频文件。 With those file, my Javascript has a for loop to create a button for each individual video found in that directory, then it puts the name of the file in the button.有了这些文件,我的 Javascript 有一个 for 循环,为在该目录中找到的每个单独的视频创建一个按钮,然后将文件的名称放在按钮中。 And all that works, now what I'm struggling with is creating an onclick event that gets the ID of the button that was pressed.所有这些都有效,现在我正在努力创建一个 onclick 事件,该事件获取按下的按钮的 ID。 I'm really struggling on doing this so any help would be appreciated.我真的很努力做到这一点,所以任何帮助将不胜感激。

My javascript:我的 javascript:

var animepaths = [];
var animenames = [];

//FILE UPLOADING
const fileSelector = document.getElementById('file-selector');
fileSelector.addEventListener('change', (event) => {
  const fileList = event.target.files;
  filesLoop(fileList)
});

//loops through every file found in the directory
//and saves the path and file name to local storage
function filesLoop(files){
    for(var x = 0; x < files.length; x++){
        animepaths.push(files[x].webkitRelativePath)
        animenames.push(files[x].name)
    }
    printOnScreen(animenames, animepaths)
}


//Creating a button with an H2 tag inside
//Then display it on screen in the container (display grid)
function printOnScreen(animenames, animepaths){
    for(var x = 0; x < animenames.length; x++){
        const elem = document.createElement('button');
        elem.classList.add("grid-item");

        const elemtext = document.createElement('h2')
        elemtext.innerHTML = animenames[x]
        elemtext.classList.add("grid-innertext")
        elem.appendChild(elemtext)
        document.getElementById('container').appendChild(elem);
    }
}

In any DOM event handler, the element that triggered the event is available within the handler via the this keyword.在任何 DOM 事件处理程序中,触发事件的元素都可以通过this关键字在处理程序中使用。 Therefore, to get the id of the button that triggered the event, you'd just use: this.id .因此,要获取触发事件的按钮的id ,您只需使用: this.id

Here's an example:这是一个例子:

 document.querySelector("button").addEventListener("click", function(){ console.log("The button's id is: " + this.id); });
 <button id="btn">Click Me</button>

If you have more than 1 button you should identify the group of buttons via a class not an id.如果您有多个按钮,则应通过 class 而不是 id 来识别按钮组。

in your case it's even easier, as you create the button pro grammatically, so we could create the event there...在您的情况下,它更容易,因为您以编程方式创建按钮,所以我们可以在那里创建事件......

//your function and some of my code
function printOnScreen(animenames, animepaths){
    for(var x = 0; x < animenames.length; x++){
       createAndAppendButton(animenames[x], animepaths[i]);
    }
}

function createAndAppedButton(name, path) {
  let button = document.createElement('button');
  button.classList.add("grid-item");
  button.innerText = name
  
  document.getElementById('container').appendChild(button);

  button.addEventListener("click", function() { 
    //do something with the path, which is accessible her
    console.log(path)
  });

}

As you can see I removed your h1, as H1 cannot be a child of the button-tag如您所见,我删除了您的 h1,因为 H1 不能是按钮标签的子标签

Maybe you can use something like:也许你可以使用类似的东西:


function onClick(animenameId) {
  ...
  // your magic here
  ... 
}

function printOnScreen(animenames, animepaths){
  ...
  elem.onclick =  onClick(animenames[x]);
  ...
}
 

What do you think?你怎么看?

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

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