简体   繁体   English

使用JavaScript添加微数据

[英]Add microdata using javascript

I have a rating system based on Javascript and php. 我有一个基于Javascript和php的评分系统。 On each page is displays the result "X votes (moyenne X)". 在每个页面上显示结果“ X票(moyenne X)”。 X are numbers and "moyenne" means "average notation". X是数字,“ moyenne”表示“平均符号”。

I want the javascript to add microdata info. 我希望JavaScript添加微数据信息。 Source code should show somemething like: <span itemprop="reviewCount">X</span> (Moyenne <span itemprop="ratingValue">X</span>) 源代码应显示以下内容: <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 + ')' ); 我认为我必须更改的行是: $(widget).find('.total_votes').text( votes + ' votes (Moyenne ' + exact + ')' );

Here is my JS: 这是我的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. 我的理解是,如果Google使用Javascript添加了这些信息,那么Google和其他微数据消费者将不会获取这些信息。 I think it needs to be present in the actual HTML markup. 我认为它必须出现在实际的HTML标记中。

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; Google在各种支持页面上指出,搜寻器仅看到文本浏览器或关闭Javascript的浏览器可见的内容; I'm assuming that it's no different when Microdata is involved. 我假设涉及到微数据时也没有什么不同。 See here and here for example. 例如,请参见此处此处

only Opera n FF supports the MicroData JS. 只有Opera n FF支持MicroData JS。

<span itemprop="reviewCount">X</span> (Moyenne <span itemprop="ratingValue">X</span>)

Here is ur JS Code 这是您的JS代码

var span = document.createElement("span");
span.itemProp = "reviewCount";
span.src = "http://example.org/example.jpg";
span.itemValue = X;

var span1 = document.createElement("span");
span1.itemProp = "ratingValue";
span1.src = "http://example.org/example.jpg";
span1.itemValue = X;

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

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