JS脚本语言,如何定位在两个不同文本框中?
var helpstat = false;
var basic = false;
var text = " ";
var AddTxt = " ";
var word= "文字 ";
var userAgent = navigator.userAgent.toLowerCase();
var is_ie = userAgent.indexOf( 'msie ') != -1;
var mydocum;
function init(){
mydocum=document.myform.mytext;
}
//function enable(btn){btn.filters.gray.enabled=0;}
//function disable(btn){btn.filters.gray.enabled=1;}
function bold() {
if (helpstat) {alert( "加粗标记\n使文本加粗.\n用法: [b]这是加粗的文字[/b] ");
}
else if (basic) {
lycode( "b ", " ", " ", " ");
}
else {
lycode( "b ", " ", "文字将被变粗. ",word);
}
}
function getActiveText() {
setfocus();
if (!is_ie || (is_ie && !document.selection)) {
return false;
}
var sel = document.selection;
var rng = sel.createRange();
rng.colapse;
if (rng != null && (sel.type == "Text " || sel.type == "None ")) {
text = rng.text;
}
if (mydocum.createTextRange) {
mydocum.caretPos = rng.duplicate();
}
return true;
}
function setfocus() {
mydocum.focus();
}
function AddText(NewCode) {
if (typeof(mydocum.createTextRange) != "undefined " && mydocum.caretPos) {
var caretPos = mydocum.caretPos;
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? NewCode + ' ' : NewCode;
} else {
mydocum.value += NewCode;
}
setfocus();
getActiveText();
AddTxt = " ";
}
function lycode(lycode, optioncompiled, prompttip, prompttext) {
if(optioncompiled!= ' ') {
optioncompiled= "= "+optioncompiled;
}
// lets call this when they try and use lycode rather than on change
getActiveText();
if (text) {
// its IE to the rescue
if (text.substring(0, lycode.length + 2 ) == "[ " + lycode + "] " && text.substring(text.length - lycode.length - 3, text.length) == "[/ " + lycode + "] ") {
AddTxt = text.substring(lycode.length + 2, text.length - lycode.length - 3);
} else {
AddTxt = "[ " + lycode + optioncompiled + "] " + text + "[/ " + lycode + "] ";
}
AddText(AddTxt);
} else if (mydocum.selectionEnd && (mydocum.selectionEnd - mydocum.selectionStart > 0)) {
// its mozilla and we 'll need to re-write entire text
var start_selection = mydocum.selectionStart;
var end_selection = mydocum.selectionEnd;
if (end_selection <= 2) {
end_selection = mydocum.textLength;
}
// fetch everything from start of text area to selection start
var start = (mydocum.value).substring(0, start_selection);
// fetch everything from start of selection to end of selection
var middle = (mydocum.value).substring(start_selection, end_selection);
// fetch everything from end of selection to end of text area
var end = (mydocum.value).substring(end_selection, theform.content.textLength);
if (middle.substring(0, lycode.length + 2 ) == "[ " + lycode + "] " && middle.substring(middle.length - lycode.length - 3, middle.length) == "[/ " + lycode + "] ") {
middle = middle.substring(lycode.length + 2, middle.length - lycode.length - 3);
} else {
middle = "[ " + lycode + optioncompiled + "] " + middle + "[/ " + lycode + "] ";
}
mydocum.value = start + middle + end;
}
else {
if(prompttip== ' ') {
AddTxt = "[ " + lycode + optioncompiled + "] "+prompttext+ "[/ " + lycode + "] ";
} else {
var inserttext = prompt(prompttip, prompttext);
if (inserttext != null) {
AddTxt = "[ " + lycode + optioncompiled + "] " + inserttext + "[/ " + lycode + "] ";
}
}
AddText(AddTxt);
}
setfocus();
return false;
}
jsp页面如下:
<BODY leftMargin=0 topMargin=0 onload=init()> <IMG class=gray onclick=bold() height=22 alt=粗体字 src= 'images/jsFont/bold.gif ' width=23>
我现在在同一页面有两个FORM,每个FORM中有一个TEXTEREA
现在的要求是:
两个textarea上分别有一个图片:images/jsFont/bold.gif,点击其中一图片,在对应的textarea中变换字体?
[解决办法]
JavaScript?有这个版,你可以再那里看看:WEB开发-> JavaScript
