简体   繁体   English

使用JavaScript附加HTML + RDFa

[英]Appending HTML+RDFa with JavaScript

My JavaScript script is not allowing to be marked up semantically. 我的JavaScript脚本不允许在语义上进行标记。 As you can see in my script below, I am using Schema.org and RDFa. 正如您在下面的脚本中看到的那样,我正在使用Schema.org和RDFa。

The problem is that when I validate my page, only the part before the append function is validated. 问题是,当我验证我的页面时,仅验证了append功能之前的部分。 This means that only type, headline, publisher and datePublished comes up. 这意味着只有类型,标题,发布者和datePublished出现。

How can I fix it? 我该如何解决? I suspect the problem here is the append function. 我怀疑这里的问题是append功能。

$(document).ready(function(){
                        $.getJSON(webhose_request, function(results){ //send request to API and store results in "results"
                            //parse the results' from the JSON response and display them //in a div element for example <div class='webhoseapi'></div>
                            //we can loop to display all results in a for loop or using the results.posts.lenght; or just display a few.

                            for (var i = 0; i < 10; i++) {
                                // you need to read the JSON results to know how to parse them, for example here results.posts[i].text
                             var articletext = results.posts[i].text;
                              // we use regular expressions REGEX to replace any new line '\n' and carriage return '\r' with html breaks </br>
                              articletext = articletext.replace(/(?:\r\n|\r|\n)/g, '</br>');
                                $(".webhose").append('<div vocab="http://schema.org/" typeOf="Article"><div property="headline" class="whtitel">'+results.posts[i].thread.title_full.substring(0,110)+'</div><div class="source"><b>Source:</b><span property="publisher"> '+results.posts[i].thread.site+'</span></div></div>');
                                if(results.posts[i].thread.author){
                                    $(".webhose").append('<div class="whpublished"><b>By:</b> <span property ="author">'+results.posts[i].thread.author+'</span></div>');
                                }
                                $(".webhose").append('<div class="whpublished"><b>Date published:</b><em><span property="datePublished"> '+results.posts[i].thread.published.substring(0,10)+'</p></span></em> </div>');
                                //we check if there is an image for this posts then display
                                if(results.posts[i].thread.main_image){
                                    $(".webhose").append('<div class="whimage"><img property="image" src="'+results.posts[i].thread.main_image+'" height="125" width="200"/></div>');
                                }
                                $(".webhose").append('<div property="articleBody" class="wharttext">'+articletext.substr(0,500)+'... <div class="whlink"><a property="url" href= '+results.posts[i].thread.url+'> Read full article »</a></div></div><br>');
                            }
                        });
                    });

I think the issue is that most of the parts of the article aren't in the <div vocab="http://schema.org/" typeOf="Article"> element. 我认为问题在于,本文的大部分内容都不在<div vocab="http://schema.org/" typeOf="Article">元素中。 This can be shown to be true if one indents HTML from the first append: 如果一个人从第一个附加文件缩进HTML,则可以证明这是正确的:

<div vocab="http://schema.org/" typeOf="Article">
  <div property="headline" class="whtitel">'+results.posts[i].thread.title_full.substring(0,110)+'</div>
  <div class="source"><b>Source:</b><span property="publisher"> '+results.posts[i].thread.site+'</span></div>
</div>

In the following I have changed the code to create the article div and then append the contents to it and then append it to the document. 在以下内容中,我更改了代码以创建文章div,然后将内容附加到该文章,然后将其附加到文档。 The following is untested but I think it might work. 以下未经测试,但我认为可能有效。

$(document).ready(function() {
  $.getJSON(webhose_request, function(results) { //send request to API and store results in "results"
    //parse the results' from the JSON response and display them //in a div element for example <div class='webhoseapi'></div>
    //we can loop to display all results in a for loop or using the results.posts.lenght; or just display a few.

    for (var i = 0; i < 10; i++) {
      // you need to read the JSON results to know how to parse them, for example here results.posts[i].text
      var articletext = results.posts[i].text;
      // we use regular expressions REGEX to replace any new line '\n' and carriage return '\r' with html breaks </br>
      articletext = articletext.replace(/(?:\r\n|\r|\n)/g, '</br>');

      var article = $('<div vocab="http://schema.org/" typeOf="Article"></div>');

      article.append('<div property="headline" class="whtitel">' + results.posts[i].thread.title_full.substring(0, 110) + '</div>')
      article.append('<div class="source"><b>Source:</b><span property="publisher"> ' + results.posts[i].thread.site + '</span></div>');)
      if (results.posts[i].thread.author) {
        article.append('<div class="whpublished"><b>By:</b> <span property ="author">' + results.posts[i].thread.author + '</span></div>');
      }
      article.append('<div class="whpublished"><b>Date published:</b><em><span property="datePublished"> ' + results.posts[i].thread.published.substring(0, 10) + '</p></span></em> </div>');
      //we check if there is an image for this posts then display
      if (results.posts[i].thread.main_image) {
        article.append('<div class="whimage"><img property="image" src="' + results.posts[i].thread.main_image + '" height="125" width="200"/></div>');
      }
      article.append('<div property="articleBody" class="wharttext">' + articletext.substr(0, 500) + '... <div class="whlink"><a property="url" href= ' + results.posts[i].thread.url + '> Read full article »</a></div></div><br>');

      $(".webhose").append(article);
    }
  });
});

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

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