【问题标题】:How to Display Post tags separate with a tag in JavaScript?如何在 JavaScript 中显示与标签分开的帖子标签?
【发布时间】:2020-07-01 16:43:39
【问题描述】:

这是我在数据库中的数据:

{
    id:1,
    title:"post title",
    content:"Lorem ipsum dolor sit amet consectetur adipisicing elit. Recusandae, ab.",
    tags:"DISH, MENU, FOOD, TASTY"
    
}

在显示时希望将标签的每个关键字与自己的href标签分开,如下所示:

http://example.com/search?tag=[tag keyword] 
want like this >> ( http://example.com/search?tag=DISH)

我正在使用反应

want to achieve this

【问题讨论】:

    标签: javascript reactjs tags


    【解决方案1】:

    我们可以像这样拆分标签开始:

    const data = {
     id:1,
     title:"post title",
     content:"Lorem ipsum dolor sit amet consectetur adipisicing elit. Recusandae, ab.",
     tags:"DISH, MENU, FOOD, TASTY"
    }
    
    tags = data.tags.split(",");
    

    之后我们可以像这样创建链接:

    render() {
      const url = 'http://example.com/search?tag=';
      return (
        tags.map(tag => {
          <a href={`${url}${tag.trim().lowercase()}`}>{tag.trim()}</a>
        })
      )
    }
    

    【讨论】:

      【解决方案2】:

      首先使用var splitting = tags.split(",")拆分tags字段 然后使用 for 循环遍历 splitting 并形成您想要的 url/布局

      【讨论】:

        猜你喜欢
        • 2019-11-09
        • 1970-01-01
        • 2016-04-05
        • 2021-10-12
        • 1970-01-01
        • 2014-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多