日期:2014-05-17  浏览次数:20603 次

在文本内输入字,在另一个div(span)中同步出现,如何实现?
如题,最好有实例,谢谢了。

------解决方案--------------------
HTML code

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="gb2312" />
        <title></title>    
        <style>
            body {font-size:12px;}
        </style>        
    </head>
    <body>
        <input id="text" />
        <div id="div"></div>
        <script>
            function $(el){
                return typeof el == 'string' ? document.getElementById(el) : el;
            }
            $('text').onkeyup = function(){
                $('div').innerHTML = this.value;
            }
        </script>
    </body>
</html>

------解决方案--------------------
HTML code
<input id='i'/>
<div id='d' style='width:300px; height:50px; border:1px solid #ccc'></div>
<script>
var ipt = document.getElementById('i');
var div = document.getElementById('d');
ipt.onkeyup = function()
{
     div.innerHTML = this.value
}
</script>

------解决方案--------------------
<input type="text" onkeyup="toSpan(this);" /><span id="span1"></span>
<script type="text/javascript">
function toSpan(obj) {
document.getElementById('span1').innerHTML = obj.value;
}
</script>
------解决方案--------------------
HTML code

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="gb2312" />
        <title></title>    
        <style>
            body {font-size:12px;}
        </style>        
    </head>
    <body>
        <input id="text" />
        <div id="div"></div>
        <script>
            function $(el){
                return typeof el == 'string' ? document.getElementById(el) : el;
            }
            $('text').onkeyup = function(){
                if( this.value.length > 6 ){
                    $('div').innerHTML =  this.value.substring(0,6) + '...';
                }else{
                    $('div').innerHTML = this.value;
                }  
            }
        </script>
    </body>
</html>