Add microdata using javascript

巧了我就是萌 提交于 2019-12-11 03:08:44

问题


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>

回答1:


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.




回答2:


only Opera n FF supports the MicroData JS.

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

Here is ur JS Code

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;


来源:https://stackoverflow.com/questions/11923447/add-microdata-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!