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/