0

我有这段代码来检索某个文本 div 的字体系列,以便将其设置为我的编辑器的下拉 (SELECT) 字体选择器:

    //getter for fontFamily
    function getFontFamily(inner) {
        var fontFamilyVal = $(inner).css("font-family");
        console.log('current font family is ' + fontFamilyVal)
        $("select#fontFamily option").each(function() {
            this.selected = (this.value == fontFamilyVal); 
        });
    };

但是它并不总是在 Chrome 中工作。它仅适用于没有空格的字体名称,当我有一个名称有空格的字体系列时它会失败(但在 Firefox 中可以正常工作)。

4

1 回答 1

1

调试语句帮助...

console.log('current font family is ' + fontFamilyVal)

...并透露,如果字体名称中有空格,Chrome 会在字体名称周围添加 '(Firefox 没有)。所以这是我修改代码以在 chrome 中也能工作的方法:

    //getter for fontFamily
    function getFontFamily(inner) {
        var fontFamilyVal = $(inner).css("font-family");
        console.log('current font family is ' + fontFamilyVal)
        $("select#fontFamily option").each(function() {
            if( (this.value == fontFamilyVal) || (this.value == "'"+fontFamilyVal+"'") ) {
                this.selected = true;   
            }
        });
    };
于 2013-10-05T21:18:42.160 回答