2006.02.20 Monday | 19:07

  テキストエリアの選択部分の置き換え 記録

function ReplaceTextarea() {

	var obj,objSel,strValue,nStart,nEnd;

	obj = document.getElementsByName("text")[0];
	if ( document.all ) {
		objSel = document.selection;
		obj.focus();
		strValue = objSel.createRange().text;
		if ( strValue == '' ) {
			return;
		}
		objSel.createRange().text = "<b>" + strValue + "</b>";
	}
	else {
		if ( obj.selectionEnd == obj.selectionStart ) {
			return;
		}
		nStart = obj.selectionStart;
		nEnd = obj.selectionEnd;
		strValue = obj.value.substring(
			nStart,
			nEnd
		);
		strValue =
			obj.value.substring(0, nStart) +
			"<b>" +
			strValue +
			"</b>" +
			obj.value.substring(nEnd,obj.value.length);
		obj.value = strValue
		obj.setSelectionRange(
			nStart,
			nEnd + 7);
		obj.focus();
	}
}



<< 2/2