简体   繁体   English

如何从点击事件中获取 id

[英]How to get the id from a click event

I use query snapshot.forEach created something like for loop that will loop through all of my data in firebase, and print them out with a div tag with a click event.我使用查询snapshot.forEach创建了类似于 for 循环的东西,它将遍历 firebase 中的所有数据,并使用带有点击事件的 div 标签打印出来。 When other users click on the div event, the event will grab the document in cloud firestore with the id of the div tag, now I am looking for how to get the id from that div tag that triggered the click event.当其他用户点击 div 事件时,该事件将使用 div 标签的 id 抓取 cloud firestore 中的文档,现在我正在寻找如何从触发点击事件的 div 标签中获取 id。

db.collection("posts")
    .get()
    .then((querySnapshot) => {
        querySnapshot.forEach((doc) => {
        postData = doc.data()
        var wrapper = document.createElement('div')
        wrapper.classList.add('wrapper')
        wrapper.setAttribute('id',postData.title);
        var content = document.createElement('div')
        content.classList.add('content')
        content.setAttribute('id',postData.id);
        var title = document.createElement('h1')
        title.classList.add('title')
        var introduction = document.createElement('p')
        introduction.classList.add('introduction')
        title.innerHTML = postData.title
        introduction.innerHTML = postData.introduction
        postCollection.appendChild(wrapper)
        wrapper.appendChild(content)
        content.appendChild(title)
        content.appendChild(introduction)
        content.onclick=function(event){
      profilePage.classList.add('hide')
      forumePage.classList.add('hide')
      postPage.classList.add('hide')
      postComment.classList.remove('hide')
        }
            console.log(doc.id, " => ", doc.data());
        });
    })

在此处输入图像描述

I think that should answer your question, or at least what I understood from it我认为这应该回答你的问题,或者至少我从中理解的

db.collection('posts')
  .get()
  .then ( querySnapshot =>
    {
    querySnapshot.forEach( doc =>
      {
      let postData = doc.data()
        , wrapper  = document.createElement('div')
        , content  = document.createElement('div')
        ;
      wrapper.className = 'wrapper'
      wrapper.id        = postData.title
      content.className = 'content'
      content.id        = postData.id
      content.innerHTML = `
        <h1 class="title">${postData.title}</h1>
        <p class="introduction">${postData.introduction}</p>`

      wrapper.appendChild(content)
      postCollection.appendChild(wrapper)

      content.onclick = event =>   // in case of way 1 you need to use Arrow function
        {
        profilePage.classList.add('hide')
        forumePage.classList.add('hide')
        postPage.classList.add('hide')
        postComment.classList.remove('hide')
        console.log(content.id)               // way 1
        console.log(event.currentTarget.id)   //  way 2
        }
      console.log( doc.id, " => ", doc.data())
      })
    })

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

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