【问题标题】:How to dynamically load the content according to the user id in javascript如何在javascript中根据用户id动态加载内容
【发布时间】:2021-08-23 15:11:17
【问题描述】:

我已经动态创建了指向用户 ID 的链接 当我单击链接时,将显示页面。 我想获取数据并将其添加到模板中以根据用户ID动态加载内容。

function displayPhotographers(jsonfilter){
  
        // jsonfilter est un tableau d'objets
        
        let photographerDetails = "";
        
        jsonfilter.forEach((photographer)=>{ 

          const str = photographer.name;
          const dash = str.replace(" ", "-");
              // Il construit son HTML
              photographerDetails += `
                <div class="user" id=${photographer.id}>
                  <a href="/views/page.html?${photographer.id}">
                  <div class="circle thumb">
                      <div class="crop">
                      <img src="img/${photographer.portrait}" alt="" />
                      </div>
                      <h2 class="name">${photographer.name}</h2>
                  </div>
                  </a>
                  <p class="city">${photographer.city}</p>
                  <p class="tagline">${photographer.tagline}</p>
                  <p class="price">${photographer.price} €/jour</p>
                  <ul class="tags">
                      ${photographer.tags
                        .map((tag) =>
                          `
                      <li>
                          <a href="#" class="${tag}">#${tag}</a>
                      </li>
                  `.trim()
                        )
                        .join("")}
                  </ul>
                </div>
              `;
        })

        return photographerDetails;
       
      
      }

【问题讨论】:

  • 您是否尝试从您链接到的页面的 url 中获取 id?如果是这种情况,那么以下内容可能会对您有所帮助:var currentUrl = window.location.href
  • 是的,谢谢您的回答。

标签: javascript api using


【解决方案1】:

比如:

function get_id(){
  var currentUrl = window.location.href;
  var tagLocation = currentUrl.indexof("#");
  if(tagLocation<0){
   console.warn("no id could be found");
   return -1;
  }else{
    return currentUrl.substr(tagLocation);
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    相关资源
    最近更新 更多