js代碼:
復制代碼 代碼如下:
window.onload = function(){
var mian = document.getElementById( "mian" );
var mian1 = document.getElementById( "mian1" );
var str = mian.innerHTML;
var arry = [];
var len = str.length;
for( var i = 0; i < len; i++ ){
arry[ i ] = str[ i ];
};
function paix1( arry ){
var temp;
while( len > 0 ){
for( var j = 0; j < len - 1; j++ ){
if( arry[ j ] > arry[ j+1 ] ){
temp = arry[ j ];
arry[ j ] = arry[ j + 1 ];
arry[ j + 1 ] = temp;
};
};
len--;
};
return arry;
};
mian1.innerHTML = paix1(arry).join(""); // 把數組轉換成為字符串
};
html代碼:
復制代碼 代碼如下:
<div id="mian">vdwsxdfrthspas</div>
<div id="mian1"></div>