如何使这个textarea javascript在除Internet Explorer之外的其他浏览器中工作

时间:2011-10-22 16:41:54

标签: javascript

请有人帮我解决这个问题

这是一个JavaScript代码,当用户在V Bulletin和SMF论坛中点击它时,在textarea框中插入特定的bbcode [b] [/ b],当用户点击带有<的图像时strong> B [b] [/ b]以textarea格式出现,

现在我让我工作,虽然我不擅长JavaScript 但问题是它只适用于 INTERNET EXPLORER ,并且在OPERA和Mozilla中不起作用 所以我需要帮助才能使其在除IE之外的其他浏览器中工作

<HTML>
 <Head>
 <script type="text/javascript">

 var isSelected = false;

 function copySelection(workArea){

 workArea.workText = document.selection.createRange();
 isSelected = true;
 }

 function wrapInTags(workArea,isTag){

 if (isSelected)
 {
 workArea.workText.text = "["+isTag+"]"+workArea.workText.text+"[/"+isTag+"]";
 if (workArea.workText.text==''){isSelected=false;workArea.focus()}
 }
 }

 function AddURL(workArea){

 var AddURL="";
 var txt="";


 txt=prompt("Enter URL for the link.","http://");
 AddURL="[.url="+txt+"]";
 AddURL2="[./url]";


 if (isSelected)
 {
 workArea.workText.text = AddURL+workArea.workText.text+AddURL2;
 if (workArea.workText.text==''){isSelected=false;workArea.focus()}
 }

 }

 </Script>
 </Head>


 <Body>
 <Form name='Form1' method=post action='process.php'>
 <textarea name='area1' cols='40' rows='4' onselect="copySelection(this)"></textarea><br>
 <input type=button value='Italic' onclick="wrapInTags(this.form.area1,'i')">
 <input type=button value='Bold' onclick="wrapInTags(this.form.area1,'b')">
 <input type=button value='Underline' onclick="wrapInTags(this.form.area1,'u')">
 <input type=button value='Link' onclick="AddURL(this.form.area1)">
 <br><br>
 <input type=submit value='Submit'>
 </Form>


 </Body>
 </HTML>

1 个答案:

答案 0 :(得分:1)

document.selection 及其所有方法和标准仅限IE,但还有其他一些在其他浏览器上运行良好的东西。

var isSelected = false;

function copySelection(workArea){
    workArea.workText = document.selection ? document.selection.createRange() : {text:workArea.value.substring(workArea.selectionStart,workArea.selectionEnd)};
    workArea.offsetSelection = workArea.selectionStart;
    isSelected = true;
}


function wrapInTags(workArea,isTag){
    if (isSelected)
        {
        if(document.selection){ 
            workArea.workText.text = "["+isTag+"]"+workArea.workText.text+"[/"+isTag+"]";
        } else {        
            workArea.value = 
                workArea.value.substring(0,workArea.offsetSelection)
                + "["+isTag+"]"+workArea.workText.text+"[/"+isTag+"]" 
                + workArea.value.substring(workArea.offsetSelection+workArea.workText.text.length)
        }


        if (workArea.workText.text==''){
            isSelected=false;workArea.focus()
        }
    }
}


function AddURL(workArea){
    var AddURL="";
    var txt="";

    txt=prompt("Enter URL for the link.","http://");
    AddURL="[.url="+txt+"]";
    AddURL2="[./url]";

    if (isSelected) {
        if(document.selection){ 
            workArea.workText.text = AddURL+workArea.workText.text+AddURL2;
        } else {
            workArea.value = 
                workArea.value.substring(0,workArea.offsetSelection)
                +  AddURL +workArea.workText.text+ AddURL2
                + workArea.value.substring(workArea.offsetSelection+workArea.workText.text.length)
        }

        if (workArea.workText.text==''){
            isSelected=false;workArea.focus()
        }
    }

}

大卫指出的解决方案也很有效。