| Current Path : /home/zqegovsj/www/us3web.haibo.com.cn/biguo/image/js/ |
| Current File : /home/zqegovsj/www/us3web.haibo.com.cn/biguo/image/js/savePos.js |
//--------------------------------------savePos.js add by zhangtingchang
(function(){
var fieldSelection = {
getSelection: function(){
var e = this.jquery ? this[0] : this;
return ( /* mozilla / dom 3.0 */('selectionStart' in e &&
function(){
var l = e.selectionEnd - e.selectionStart;
return {
start: e.selectionStart,
end: e.selectionEnd,
length: l,
text: e.value.substr(e.selectionStart, l)
};
}) ||
/* exploder */
(document.selection &&
function(){
e.focus();
var r = document.selection.createRange();
if (r == null) {
return {
start: 0,
end: e.value.length,
length: 0
}
}
var re = e.createTextRange();
var rc = re.duplicate();
re.moveToBookmark(r.getBookmark());
rc.setEndPoint('EndToStart', re);
return {
start: rc.text.length,
end: rc.text.length + r.text.length,
length: r.text.length,
text: r.text
};
}) ||
/* browser not supported */
function(){
return {
start: 0,
end: e.value.length,
length: 0
};
})();
},
replaceSelection: function(){
var e = this.jquery ? this[0] : this;
var text = arguments[0] || '';
return ( /* mozilla / dom 3.0 */('selectionStart' in e &&
function(){
e.value = e.value.substr(0, e.selectionStart) + text + e.value.substr(e.selectionEnd, e.value.length);
return this;
}) ||
/* exploder */
(document.selection &&
function(){
e.focus();
document.selection.createRange().text = text;
return this;
}) ||
/* browser not supported */
function(){
e.value += text;
return this;
})();
}
};
jQuery.each(fieldSelection, function(i){
jQuery.fn[i] = this;
});
})();
(function($){
$.fn.charLimit = function(options){
var defaults = {
limit: 30,
speed: "normal",
descending: true
}
var o = $.extend(defaults, options);
return this.each(function(i){
var obj = $(this);
if (!obj.next().hasClass("countBox"))
obj.after("<span class='countBox box" + i + "' style='display:none;padding:1px 2px;font-size: 13px;border:1px solid #aaa;'></span>");
function countChars(){
var value = (o.descending) ? o.limit - obj.val().length-obj.val().split('\n').length : obj.val().length+obj.val().split('\n').length;
$(".box" + i).text(value);
value < 0 ? $(".box" + i).css('background', 'yellow') : $(".box" + i).css('background', 'white');
}
countChars();
obj.keydown(function(e){
/*
if ( e.keyCode != "8" && e.keyCode != "9" && e.keyCode != "46")
e.preventDefault(); // cancel event
*/
countChars();
}).keyup(function(e){
countChars();
}).focus(function(){
obj.next().fadeIn(o.speed);
countChars();
}).blur(function(){
obj.next().fadeOut(o.speed);
});
});
}
})(jQuery);
var start = 0;
var end = 0;
function saveKeyPos(e, txtBox){
var key = (window.event) ? window.event.keyCode : e.keyCode;
if (key == 37 || key == 38 || key == 39 || key == 40) {
savePos(txtBox);
}
}
function setPos(txb, start, end){
var range = txb.createTextRange();
range.collapse(true);
range.moveEnd("character", end);
range.moveStart("character", start);
range.select();
txb.focus();
}
$(document).ready(function(){
$('textarea').keydown(update).keyup(update).mousedown(update).mouseup(update).focus(update);
if ($('#prodModel').size() == 1) {
$('textarea').charLimit({
limit: 4000,
speed: "fast",
descending: true
});
}
});
function update(){
var range = $(this).getSelection();
document.getElementById("sp").value = range.start;
document.getElementById("ep").value = range.end;
}
function insert4ff(txtarea, text){
var scrollPos = txtarea.scrollTop;
var strPos = 0;
var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ? "ff" : (document.selection ? "ie" : false));
strPos = 1*document.getElementById("sp").value ;
var front = (txtarea.value).substring(0, strPos);
var back = (txtarea.value).substring(strPos, txtarea.value.length);
txtarea.value = front + text + back;
strPos = strPos + text.length;
txtarea.selectionStart = strPos;
txtarea.selectionEnd = strPos;
txtarea.focus();
txtarea.scrollTop = scrollPos;
}