使用javascript添加微数据 [英] Add microdata using javascript

查看:75
本文介绍了使用javascript添加微数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Javascript和php的评分系统。在每个页面上显示结果X票(moyenne X)。 X代表数字,moyenne代表平均符号。

我想要javascript来添加微数据信息。源代码应该显示如下:< span itemprop =reviewCount> X< / span> (Moyenne< span itemprop =ratingValue> X< / span>)



你可以帮我吗?



我认为我必须改变这一行: $(widget).find( '.total_votes')。text(votes +'votes(Moyenne'+ exact +')');





 < script> 
$(document).ready(function(){
$('。rate_widget')。each(function(i){
var widget = this;
var out_data = {
widget_id:$(widget).attr('id'),
fetch:1
};
$ .post(
'../php/ ($ INFO);
set_votes(widget); $ b $($);
out_data,
function(INFO){
$(widget).data('fsr',INFO) b
$ j


$ b $('。ratings_stars')。hover(
function(){$ ('ratings_vote');
},$($) b $ b函数(){
$(this).prevAll()。andSelf()。removeClass('ratings_over');
set_votes($(this).parent());

);

$('。ratings_stars')。bind('click ',function(){
var star = this;
var widget = $(this).parent();
var clicked_data = {
clicked_on:$(star).attr('class'),
widget_id:$(star).parent()。attr('id')
};
$ .post(
'../php/ratings.php',
clicked_data,
function(INFO){
widget.data('fsr', INFO);
set_votes(widget);
},
'json'
);
});
});

函数set_votes(widget){
var avg = $(widget).data('fsr')。whole_avg;
var votes = $(widget).data('fsr')。number_votes;
var exact = $(widget).data('fsr')。dec_avg;
window.console&& console.log('现在在set_votes中,它认为fsr是'+ $(widget).data('fsr')。number_votes);
$(widget).find('。star_'+ avg).prevAll()。andSelf()。addClass('ratings_vote');
$(widget).find('。star_'+ avg).nextAll()。removeClass('ratings_vote');
$(widget).find('。total_votes')。text(votes +'votes(Moyenne'+ exact +')');
}
< / script>


解决方案

我的理解是Google和其他微数据消费者不会如果它是通过Javascript添加的,请选取信息。我认为它需要出现在实际的HTML标记中。



Google在各种支持页面上指出,抓取工具只能查看文本浏览器可见的内容, JavaScript关闭的浏览器;我假设Microdata参与时没有什么不同。请参阅此处此处为例。


I have a rating system based on Javascript and php. On each page is displays the result "X votes (moyenne X)". X are numbers and "moyenne" means "average notation".

I want the javascript to add microdata info. Source code should show somemething like: <span itemprop="reviewCount">X</span> (Moyenne <span itemprop="ratingValue">X</span>)

Is it possible ? And could you help me ?

I think the line I have to change is this one: $(widget).find('.total_votes').text( votes + ' votes (Moyenne ' + exact + ')' );

Here is my JS:

<script>
$(document).ready(function() {
    $('.rate_widget').each(function(i) {
        var widget = this;
        var out_data = {
            widget_id : $(widget).attr('id'),
            fetch: 1
        };
        $.post(
            '../php/ratings.php',
            out_data,
            function(INFO) {
            $(widget).data( 'fsr', INFO );
                set_votes(widget);
            },
            'json'
        );
    });

    $('.ratings_stars').hover(
        function() {
            $(this).prevAll().andSelf().addClass('ratings_over');
            $(this).nextAll().removeClass('ratings_vote'); 
        },
        function() {
            $(this).prevAll().andSelf().removeClass('ratings_over');
            set_votes($(this).parent());
        }
    );

    $('.ratings_stars').bind('click', function() {
        var star = this;
        var widget = $(this).parent();
        var clicked_data = {
            clicked_on : $(star).attr('class'),
            widget_id : $(star).parent().attr('id')
        };
        $.post(
            '../php/ratings.php',
            clicked_data,
            function(INFO) {
                widget.data( 'fsr', INFO );
                set_votes(widget);
        },
            'json'
        ); 
    });
});

function set_votes(widget) {
    var avg = $(widget).data('fsr').whole_avg;
    var votes = $(widget).data('fsr').number_votes;
    var exact = $(widget).data('fsr').dec_avg;
    window.console && console.log('and now in set_votes, it thinks the fsr is ' + $(widget).data('fsr').number_votes);
    $(widget).find('.star_' + avg).prevAll().andSelf().addClass('ratings_vote');
    $(widget).find('.star_' + avg).nextAll().removeClass('ratings_vote'); 
    $(widget).find('.total_votes').text( votes + ' votes (Moyenne ' + exact + ')' );
}
</script>

解决方案

My understanding is that Google and other microdata consumers will not pick up the information if it is added by Javascript. I think it needs to be present in the actual HTML markup.

Google states on various support pages that crawlers see only the content that is visible to a text browser, or a browser with Javascript turned off; I'm assuming that it's no different when Microdata is involved. See here and here for example.

这篇关于使用javascript添加微数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆