简体   繁体   English

文字向上/向下? 不对。 JavaScript循环。

[英]Text is going up/down? Not across. Javascript Loop.

<html>
<body>
<script>
var namIn = window.prompt("Enter Senator’s State and FULL Name, separated by space:" );
var namAr = namIn.split("");
var namArLen = namAr.length;

document.write( namAr + "<br>" + "Length:" + namArLen);
var i;
for (i = namArLen-1; i >=0;i--)
{var result = document.write("<br>" + "<h1>" + namAr[i] + "</h1>" ); }
</script> 
</body> 
</html>

I used a loop to help reverse my text (done for an into to JS assginment) but the letters are going in a straight line up and down. 我使用一个循环来帮助反转文本(完成对JS组合的处理),但字母沿直线上下移动。 Like: 喜欢:
O Ø
L 大号
L 大号
E Ë
H H
(It's supposed to be a senator's name, but "hello" will do for an example) (它应该是参议员的名字,但“ hello”将作为示例)
Why is this and how can I make it look normal? 为什么会这样,如何使它看起来正常?

You can make it look normal by putting the letters into another variable and then writing that variable out all at once. 通过将字母放入另一个变量,然后一次将其全部写出,可以使其看起来正常。 Look at lines 6, 9 and 11 in this (tested) solution. 查看此(经过测试的)解决方案中的第6、9和11行。

<script>
var namIn = window.prompt("Enter Senator’s State and FULL Name, separated by space:" );
var namAr = namIn.split("");
var namArLen = namAr.length;

document.write( namAr + "<br>" + "Length:" + namArLen);
var result =  '';
var i;
for (i = namArLen-1; i >=0;i--) {
    result = result + namAr[i];
}
document.write("<br>" + "<h1>" + result + "</h1>" );
</script> 

You are using H1 tags which create new lines 您正在使用H1标签创建新行

{var result = document.write( namAr[i]  ); }

use a style to increase the font size 使用样式增加字体大小

example: http://jsfiddle.net/sanpopo/rk6Nk/ 示例: http//jsfiddle.net/sanpopo/rk6Nk/

Maybe, you should add one css rule, for example: 也许,您应该添加一个css规则,例如:

h1 {display: inline-block;}

And remove all of BR tags. 并删除所有BR标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM