+ -
当前位置:首页 → 问答吧 → js alert 换行问题

js alert 换行问题

时间:2011-10-26

来源:互联网

HTML code

<!doctype html>
<html>
    <head>
        <meta charset="gb2312" />
        <title></title>
        <style>
        </style>
    </head>
    <body>
        <div msg="123\n123" id="test">i am a div</div>
        <script>
            function $(o){return document.getElementById(o)}
            
            alert( $('test').getAttribute('msg') ); // 为什么这个alert内容不换行
            
            var s = '123\n123';
            alert( s )  // 这里可以换行
            
        </script>
    </body>
</html>


作者: zgzglike   发布时间: 2011-10-26

\n是js的转义,不是属性的. 属性内\是就是 js的\\
HTML code

<!doctype html>
<html>
    <head>
        <meta charset="gb2312" />
        <title></title>
        <style>
        </style>
    </head>
    <body>
        <div msg="123
123" id="test">i am a div</div>
        <script>
            function $(o){return document.getElementById(o)}
            alert( $('test').getAttribute('msg') ); // 为什么这个alert内容不换行
            var s = '123\n123';
            alert( s )  // 这里可以换行
            
        </script>
    </body>
</html>

作者: hookee   发布时间: 2011-10-26

又学习了

作者: ifandui   发布时间: 2011-10-26

引用 1 楼 hookee 的回复:
\n是js的转义,不是属性的. 属性内\是就是 js的\\

HTML code

<!doctype html>
<html>
<head>
<meta charset="gb2312" />
<title></title>
<style>
</style>
</head>
<body……



这样在html 里回车,会影响 html 结构 啊

有其他方法吗?

作者: zgzglike   发布时间: 2011-10-26

HTML code

<!doctype html>
<html>
    <head>
        <meta charset="gb2312" />
        <title></title>
        <style>
        </style>
    </head>
    <body>
        <div msg="123\n123" id="test">i am a div</div>
        <script>
            function $(o){return document.getElementById(o)}
            alert( $('test').getAttribute('msg').replace(/\\n/g,"\n") );
             var s = '123\n123';
            alert( s )  // 这里可以换行
            
        </script>
    </body>
</html>



没什么影响吧

作者: hookee   发布时间: 2011-10-26

学习。

作者: qqstrives   发布时间: 2011-10-26