-1

使用警报调试,执行了 if 的分支。但是匹配的代码标签没有获得“数据语言”属性。

divs.html(function (index, oldhtml) {
    var newhtml = converter.makeHtml(oldhtml);
    var pat = /\{-# CODE (.+?) #-}/i;
    $('code', newhtml).text(function (index, text) {
        var m = text.split("\n")[0].match(pat);
        if (m) {
            $(this).attr("data-language", m[1]);
        }
    });
    return newhtml;
});
4

1 回答 1

0

But the code tag that matched did not get the "data-language" attribute.

That's because you're not getting the "data-language" attribute, you're setting it.

$(this).attr("data-language", m[1]);

This sets the "data-language" attribute to whatever is stored in m[1]. Please refer to jQuery's .attr() documentation.

To get the data from the element, you'd use either:

$(this).attr('data-language');

Or (more semantically correct):

$(this).data('language');

To set data attributes, you should again make use of jQuery's .data() method:

$(this).data('language', m[1]);
于 2013-05-17T10:02:58.587 回答