【问题标题】:How to get meta tag content values and average them [closed]如何获取元标记内容值并将它们平均[关闭]
【发布时间】:2019-07-30 22:00:14
【问题描述】:

我正在开发一个使用 Schema.org 微数据作为结构化内容产品列表的网站,显然 Google 需要对所有评论进行综合评分。我怎样才能抓住所有的 <meta itemprop="ratingValue" content="#" /> 标签,然后将它们平均为一个我可以输出到的变量:

<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
  <meta itemprop="ratingValue" content="average #" />
  <meta itemprop="reviewCount" content="# reviews" />
</div> 

使用 JQuery 还是 JavaScript? (其中 # = 评分值)

【问题讨论】:

    标签: javascript html meta-tags schema.org structured-data


    【解决方案1】:

    是的,使用 jQuery 并不太难。此代码将获取所有元内容,将它们转换为整数,找到平均评分,然后在 head 元素的底部附加一些 HTML

    // first gather all the meta elements with an itemprop value of "ratingValue"
    var metas = $('meta[itemprop="ratingValue"]').get();
    
    // convert the content values of these elements to integers and put them in an array
    var ratings = metas.map((m) => ~~m.content);
    
    // calculate and round the average rating value
    var average = ~~(ratings.reduce((a,b) => a + b) / ratings.length + 0.5);
    
    // create the HTML for the aggregateRating parent div
    var aggregateRating = '<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">';
    
    // create ratingValue meta HTML using the average rating
    var ratingValue = '<meta itemprop="ratingValue" content="average ' + average + '" />';
    
    // create aggregateRating meta HTML using the rating count
    var reviewCount = '<meta itemprop="reviewCount" content="' + ratings.length + ' reviews" />';
    
    // combine these strings and a closing tag, then append the HTML to the end of head
    $('head').append(aggregateRating + ratingValue + reviewCount + '</div>');
    

    或者你甚至可以使用伯纳德方法

    $('head').append(['<div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">', '</div>'].join([
      ['ratingValue', 'average ' + ~~((r = $('meta[itemprop="ratingValue"]').get().map((m) => ~~m.content)).reduce((a, b) => a + b) / r.length + .5)],
      ['reviewCount', r.length + ' reviews']
    ].map((a, b) => ['<meta itemprop="', '" content="', '">'].map((c, d) => [c, a[d]]))).replace(/,/g, ''));
    

    【讨论】:

    • 谢谢 Omikes!您的代码非常清晰,我非常感谢注释行。我修改了 jQuery 以选择我需要注入并附加到那里的特定 div id。还删除了“平均”和“评论”这两个词——这部分是我的错,因为我在代码示例中包含了这些词;我只是想清楚我的要求,但只需要一个整数。 :P
    • 哎呀,哈哈。是的,我想这更有意义!
    【解决方案2】:

    var aggregates = document.querySelectorAll("[itemprop='aggregateRating'");
    var scores = 0;
    var n = 0;
    
    for (var i = 0; i < aggregates.length; i++) {
      scoreCurr = parseFloat(aggregates[i].querySelector("[itemprop='ratingValue']").getAttribute("content"));
      nCurr = parseFloat(aggregates[i].querySelector("[itemprop='reviewCount']").getAttribute("content"));
      scores += scoreCurr;
      n += nCurr;
    }
    
    alert(scores/n);
    <div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
      <meta itemprop="ratingValue" content="35" />
      <meta itemprop="reviewCount" content="7" />
    </div>
    
    <div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
      <meta itemprop="ratingValue" content="42" />
      <meta itemprop="reviewCount" content="10" />
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-14
      • 1970-01-01
      • 1970-01-01
      • 2021-02-13
      • 2014-01-02
      • 1970-01-01
      • 2018-07-06
      • 2020-06-15
      相关资源
      最近更新 更多