你好,
首先不好意思,没仔细看你的代码。
然后,给你一个我的解决方案:
function reverseString(str) {// 去除前后空格
str = str.replace(/(^\s+)|(\s+$)/g, '')
str = str.split('').reverse().join('')
return str
}
var str = 'abcdef'
console.log('原:' + str) // abcdef
console.log('新:' + reverseString(str)) // fedcba
希望是你想要的答案,如有疑问可追问。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />
<style type="text/css">
body{
font-size:12px
color:#663300
}
</style>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="Quan Wen">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<SCRIPT Language="Javascript">
function do_change()
{
var str = document.myform.mytext.value
var len = document.myform.mytext.value.length
var i
var mystr = ""
for (i=len-1i>=0i--)
{
mystr += str.charAt(i)
}
document.myform.changetext.value = mystr
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name="myform" action="a.html" method="POST">
<TABLE border="0">
<TR>
<TD>
你的输入:
</TD>
<TD>
倒序输出:
</TD>
</TR>
<TR>
<TD>
<TEXTAREA type="text" name="mytext"></TEXTAREA>
</TD>
<TD>
<TEXTAREA type="text" name="changetext" disabled></TEXTAREA>
</TD>
</TR>
<TR>
<TD colspan="2">
<INPUT type="button" onClick="do_change()" name="Change" value="倒序输出">
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
var n = window.pormpt('input')var str = ""
for (var i = n.length - 1 i >= 0i--){
str += n[i]
}
document.write(str)