[英]how to make this texarea javascript work in other browsers except Internet Explorer
Please can someone help me out on this 请有人可以帮我这个忙
this is a JavaScript code that inserts a particular bbcode say [b] [/b] in a textarea box when the user clicks on it like it is in V Bulletin and SMF forums, when the user clicks on an image with B [b][/b] appears in the textarea form, 这是一个JavaScript代码,当用户单击它时,就像在V Bulletin和SMF论坛中一样,当用户单击带有B [b]的图像时,会在文本区域框中插入特定的bbcode,例如[b] [/ b]。 [/ b]出现在文本区域表单中,
now i made mine to work though am not good in JavaScript but the problem is that it only works in INTERNET EXPLORER and doesn't work in OPERA and Mozilla so I need help to make it work in other browser apart from IE 现在我让我可以工作,尽管在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>
document.selection and all its methods and propierties are IE only, but there is other things that work well on other browsers. 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()
}
}
}
The solution that david pointed out also works well. 大卫指出的解决方案也很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.