JS中如何输出空格

JavaScript014

JS中如何输出空格,第1张

用正则替换,如

document.getElementById('txt_id').value=document.getElementById('txt_id').value.replace(/\s/gi,'')

HTML中默认会忽略多个空格符,值保留一个。如果需要在页面上显示多个空格符,需要用空格符对应的实体(&

nbsp)来代替;(&和nbsp之间的空格在使用时需要去掉)

比如

document.write('Hello!&

nbsp&

nbsp&

nbspWorld!')

输出到页面时,Hello和World之间会有3个空格。

<script>

function a() {

for(i=9i>0i--){

for(j=9-ij<9j++){

document.write(' ')

}

document.write("<br />")

}

}

a()

</script>

这个意思吧