Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

2008/05/07

A Tricky Level of HTML Tag


This illustration is a level of HTML tag when you wanna manipulate them between each other to show the correct layout. Especially in IE6, when you develop a drag-drop Javascript code, you should find that the overlapped Div is placed under the Select tag (a drop-down list), even using the z-index CSS. Fortunately, the issue is only caused by IE6, and a solution is appeared that the Iframe tag is higher than the Select tag so we can use a empty Iframe to apply to the Select, then use the overlaped Div over the Iframe to resolve this issue. This way can also cope with a dragging event lost, when the action is over the Iframe.

2006/10/25

如何清除Javascript Array物件的方法

原來,javascript可能取不到prototype的this物件,因此在實作Array.prototype時,只能用
this.length = 0;來改變原本物件的index,目前只能用這樣來解決。

Javascript 跟 ASCII 轉換

// javascript 的charCodeAt 是取得字串的ASCII編碼
String.charCodeAt();

//num 為十進位的ASCII編碼,此formCharCode將取得對應ASCII的字串
String.fromCharCode(num);

2006/10/17

如何計算JavaScript字元的長度

當內容有中文時,字體的寬度會佔顯示的二個位元。

String.prototype.getLength = function() {
// 先找出佔二位元的字體(例如:中文)
var zh = this.match(/[^\x00-\xff]/ig);
// this.length + match到的字元個數(意思就是中文字會算二次)
return this.length + (zh == null ? 0 : zh.length);
}

使用的方式:

function test(){
var src = "1234五";
alert(src.getLength());
}

結果會出現 : 6

如何取得游標的位置?

Mozillar取得的方式:
  • object.selectionStart;
  • object.selectionEnd;
IE取得的方式:
  • // object就是textarea的物件
  • this.object.focus();
  • var range = document.selection.createRange();
  • var stored_range = range.duplicate();
  • stored_range.moveToElementText(this.object);
  • stored_range.setEndPoint('EndToEnd', range);
  • // 此selectionStart就是游標的起點
  • this.object.selectionStart = stored_range.text.length - range.text.length;
  • // 此selectionEnd就是游標的終點
  • this.object.selectionEnd = this.object.selectionStart + range.text.length;

2006/10/14

如何取得javascript Textarea 反白的字元

取得所選取的文字
var range = document.selection.createRange();
複製
var temp=range.duplicate();
設定選取的長度為:4
range.moveStart('character',4);
設定結束點的位置,從temp的字元開始
range.setEndPoint("StartToStart",temp);
將所設定的字元長度選取反白
range.select();