在IE7中,字符串的第一个大写字母发生了断裂

Uppercase first letter of a string chrashes in IE7

本文关键字:大写字母 发生了 第一个 IE7 字符串      更新时间:2023-09-26
actualizarTituloWeb('brand name - '+seccion.toLowerCase().replace(/(?:_| |'b)('w)/g, function(str, p1) { return p1.toUpperCase()}));

,

function actualizarTituloWeb(titulo){
        $( 'title' ).html ( titulo );  
}

也试过:

function actualizarTituloWeb(titulo){
        titulo = titulo[0].toUpperCase() + titulo.substring(1);
            titulo = titulo.toLowerCase();
            $( 'title' ).text ( titulo );
        return false;
}

和section的值如'reserva', 'ofertas',…

我真的不知道为什么不工作,但这实际上使整个脚本崩溃(可以在这里进行现场测试:http://toniweb.us/gm2)在IE7和当前文档的标题都没有更新,

你知道我错过了什么吗?

编辑——

刚刚意识到问题在这一行!为什么?

titulo = titulo[0].toUpperCase() + titulo.substring(1);

请注意:我们不能在这里使用CSS来实现这一点,因为它将用于document.title

在JScript <= 5.7 (IE 7)中,您不能像访问数组一样访问字符串。你必须用String.charAt()代替。类数组访问在ES 5中是标准化的。

titulo = titulo.charAt(0).toUpperCase() + titulo.substring(1);

另外$('title').text(titulo);不工作。在版本8之前的ie中,您不能通过title-元素的textContent来设置(或获取)标题。

<html>
<head>
    <title>test</title>
</head>
<body>
    <script type='text/javascript'>
document.getElementsByTagName('title')[ 0 ].firstChild // null in IE <= 8
                                                       // IE 9 (and other browser): text node with nodeValue 'test'
    </script>
</body>
</html>
使用document.title

document.title = titulo;

只要稍微修改一下正则表达式模式,就可以实现预期的行为

actualizarTituloWeb('brand name - ' + seccion.toLowerCase().replace(/(^|'s|'-)(.)/gi, function(c) { return c.toUpperCase()}));

你为什么不这样做呢:

var str = "blah blah";
str = str.toLowerCase();
str = str[0].toUpperCase() + str.substring(1);
document.title = str;