javascript - 字符串的大写首字母在 IE7 中崩溃

标签 javascript jquery internet-explorer-7

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;
}

并且 seccion 具有诸如“reserva”、“ofertas”、.. 等值

我不太确定为什么不起作用,但这实际上使整个脚本在 IE7 中崩溃(可以在此处实时测试:http://toniweb.us/gm2)并且当前文档的标题既没有更新,

知道我错过了什么吗?

-编辑-

才发现问题出在这一行!为什么?

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 中,您无法通过标题元素的 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;

关于javascript - 字符串的大写首字母在 IE7 中崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8501845/

相关文章:

javascript - 开放层 3 : Zoom map to coordinates in array

javascript - filesaver.js 不适用于 cordova 应用程序

jquery - ie7 中 jQuery append 问题

internet-explorer-8 - CSS3 饼图 : divs with border-radius flickering on mouseover

javascript - YUI 对话框/面板在 IE iframe 中无法正确呈现

php - 如何跟踪用户在网站上的时间

Javascript - 通过 ID 获取 <li> 之间的文本

javascript - JQuery 数据表 - 使用 javascript 条件语句返回 html

css - Internet Explorer 7 中的正文溢出隐藏问题

javascript - 在按钮单击事件上渲染 React 组件