【问题标题】:How to switch between html formats depending on the type of content?如何根据内容类型在 html 格式之间切换?
【发布时间】:2015-10-21 09:00:43
【问题描述】:

我正在寻找一种解决方案,如何根据存储在我的 JSON 中的内容类型在多种 html 格式之间切换。

我现在使用AJAX和JSON来填写html格式:format-text.html.

      $.ajax({
                    url: 'api/datacontent.json',
                    dataType: 'json',
                    success: function(data) {

                        root.dataArr = data;
                        root.objectArr = root.dataArr.content;
                        root.timeEvent;     
                        root.triggeredId = -1; 


                        $.get('directives/format-text.html', function(response){
                            console.log('succes');
                        })

                        .done(function (response) {
                              // Here I fill in the blanks with a for loop
                        }

                        .fail(function (response) {
                             console.dir(response); 
                        });
}

JSON:

{
        "title":    "Data content",
        "content":  [
            {
                "type" :        "youtube",
                "timeTrigger":  5,
                "title":        "Youtube",
                "subtitle":     "YouTube Channel",
                "picture" :     "images/content/london.jpg",
                "text":         "Visit their YouTube channel here.",
            },{
                "type" :        "location",
                "timeTrigger":  25,
                "title":        "London",
                "google":       "https://www.google.nl/maps/place/Londen,+Verenigd+Koninkrijk/@51.5340878,-0.0978221,12z/data=!4m2!3m1!1s0x47d8a00baf21de75:0x52963a5addd52a99"
            },{
                "type":         "text",
                "timeTrigger":  34,
                "title":        "Sherlock Holmes",
                "subtitle":     "Detective",
                "picture" :     "images/content/sherlockholmes.jpg",
                "text":         "Sherlock Holmes is a fictional detective created by British author Sir Arthur Conan Doyle. Holmes is known for his astute logical reasoning, his ability to adopt almost any disguise, and his use of forensic science to solve difficult cases.",
                "readmore":     "https://en.wikipedia.org/wiki/Sherlock_Holmes"
            }

如您所见,我使用 format-text.html 来填写内容。但我的 JSON 文件包含多种类型的内容,例如位置、文本和外部链接,每种类型都有不同的 css 样式。 如何在这些 html 内容之间切换并填写正确的内容块?

留意一些建议。提前谢谢你。

【问题讨论】:

  • 你能不能也提供你的HTML预期输出
  • 你到底是什么意思。我要在其中切换的 HTML 标记,或者我如何将内容附加到块?

标签: javascript jquery css json


【解决方案1】:

有点混乱,你没有告诉我们你想如何在视图中对这些数据类型进行分类。

无论如何,如果 API 处理单个列表中的所有内容,那么您可以将结果分类到一个新数组中。

var formattedArr = {
  'videos' : [],
  'locations' : [],
  'texts' : []
};

for( var i =0; i <= dataContent.content.length; i++ ) {
  switch( dataContent.content[i].type ) {
    case 'youtube' : formatedArr.videos.push( dataContent.content[i] ); break;
    case 'location' :formatedArr.locations.push( dataContent.content[i] ); break;
    case 'text' : formatedArr.texts.push( dataContent.content[i] );
  }
}

然后您可以轻松地遍历类型。当然,如果 API 能够为您提供所需的格式,那就更好了。

【讨论】:

  • 对此我深表歉意。在我的 JSON 中,我有一个变量“type”,我在其中定义了它包含的内容类型。所以在 for 循环中它应该检查它是哪种内容类型,并根据它获取正确的标记文件并填写它......在你的例子中我应该怎么做?
  • 好的,这需要不止一个答案。您的应用程序架构应遵循: 1. 加载您的数据 json 2. 加载您的外部模板 3. 循环通过您的 json 和上述代码切换案例。在其中调用一个将内容加载到模板中的函数。然后您可以将 sn-ps 附加到所需的标签。
  • 你能在最基本的设置中创建一个看起来应该是什么样子的 sn-p 吗?
  • 不幸的是,sn-p 是不够的;请阅读此资源:code.tutsplus.com/tutorials/… 然后您可以单独提出剩余的问题。
猜你喜欢
  • 1970-01-01
  • 2019-03-14
  • 2016-05-13
  • 1970-01-01
  • 2012-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多