duanqiang5722 2014-10-29 22:38
浏览 177
已采纳

使用JavaScript将每行的首字母大写

I'd like to know how I could capitalize the first letter of a row in JavaScript like in this PHP code :

<?php

$textParts = explode("
", $text);
foreach ($textParts as $key => $value) {
   $textParts[$key] = ucfirst($value);
}

$text = implode("
", $textParts);

$text="namjaga da geureochi mwo narago dareugetni
ni mameul da gajyeodo naragabeorigo maneun
namjaga da geureochi mwo"

?>

<p>
<?php echo $text; ?>
</p>

In JS, I get the text into the "Roman" DIV after a tranformation :

<div id="Roman"> </div>

<script>

<?php include("../../tools/js/romantool.js"); ?>
function affichar() {
  document.getElementById('Roman').innerHTML = latin_to_roman(
    document.getElementById('Parol').innerHTML
  );
}
affichar();

</script>

Does anyone know how I should proceed to do as in the previous PHP example ? Thank you !

  • 写回答

4条回答 默认 最新

  • douci1541 2014-10-30 00:04
    关注

    In fact I would go right in between the two suggested answers so far. I would use javascript to replace the line breaks with a tag that will add structure around text e.g.

    var str = document.getElementById('Parol').innerHTML;
    str = '<span>' + str.replace(/(?:
    ||
    )/g, '</span><span>') + '<span>';
    document.getElementById('Roman').innerHTML = str;
    

    And than style it in whichever way you want using CSS, your case would be

       span {
            display: block;
        }
    
        span:first-letter {
            text-transform: uppercase;
        }
    

    Here's a fiddle

    http://jsfiddle.net/58qkwezk/1/

    Adding structure to your text will help you manipulate it much easier

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟