textarea--输出选择行之前的所有文本(倒序)

来源:互联网 发布:联通iptv网络电视怎么 编辑:程序博客网 时间:2024/06/05 09:47
<html>
<head>
<style type="text/css">
</style>
<META http-equiv="content-type" content="text/html; charset=shift_jis">
<title>TEST</title>
</head>
<script type="text/javascript">    

function FantinySel(){

        
var oV = 0;
        
var nV = 0;
        
        
var r1 = document.selection.createRange();
        
var r2 = null
        oV 
= r1.boundingHeight;
        nV 
= r1.boundingHeight;
        
if (r1.parentElement().name == "textAr"){
            
while(oV == nV){
                r2 
= r1.duplicate();
                r2.moveEnd(
"character",1);
                nV 
= r2.boundingHeight;
                
if (oV == nV){
                r1.moveEnd(
"character",1);
                }

            }

            r1.select();
        }

        oV 
= r1.boundingHeight;
        nV 
= r1.boundingHeight;
        
if (r1.parentElement().name == "textAr"){
            
while(oV == nV){
                r2 
= r1.duplicate();
                r2.moveStart(
"character",-1);
                nV 
= r2.boundingHeight;
                
if (oV == nV){
                r1.moveStart(
"character",-1);
                }

            }

            r1.select();
        }

/**/
        
if (r1.parentElement().name == "textAr"){
            output.innerHTML 
= "";
            
var rng = document.getElementById("textAr").createTextRange();
            rng.collapse();

            
if(r1.text == ""){
                r1.moveEnd(
"character",1);
            }

            
while(rng.getBoundingClientRect().bottom <= r1.getBoundingClientRect().top){
                r2 
= rng.duplicate();
                rng.moveEnd(
"character",1);
            }

            
            
if(r2 != null){
                
//output.innerHTML += "<pre>" + r2.text + "</pre>";
                var aryOutput;
                
var strOutput;
                
                aryOutput 
= r2.text.split(" ");
                strOutput 
= "";
                
                
for(i=0;i<aryOutput.length;i++){
                    strOutput 
= aryOutput[i] + " " + strOutput;
                }

                output.innerHTML 
= "<pre>" + strOutput + "</pre>";
            }

        }


    }

</script>
</head>
<body bgcolor= "#ffffff">
<form method="post" action="" name="FMlog">    
<table>
    
<tr><td>
        
<input type = button value = "Config" onclick = "FantinySel()">
    
</td></tr>
    
<tr><td>
        
<textarea name = "textAr" rows = "5" cols = "100">
123424323423
56782342423
98762342423
9876523423423
56782342423
98762342423
9876523423423
        
</textarea>
    
</td>
    
</tr>
</table>
<div id="output"></div>
</form>
</body>
</html>
 
原创粉丝点击