1.网页上获得元素的绝对位置
经常要用到的方法,其实很简单,使用 obj.offsetLeft和obj.offsetTop是获得相对父元素的位置,所以只要遍历dom树就可以获得正确的位置了。有时候需要在html控件上覆盖一个div,这种情况下就可以用到了,代码如下:
代码来自《ppk on javascript》这本书 :-)
2. ie中select的innerHtml无效的解决方法
3.动态载入js
4.ie6中css伪类hover无效的解决方法
html代码:
经常要用到的方法,其实很简单,使用 obj.offsetLeft和obj.offsetTop是获得相对父元素的位置,所以只要遍历dom树就可以获得正确的位置了。有时候需要在html控件上覆盖一个div,这种情况下就可以用到了,代码如下:
- function findPos(obj) {
- var curleft = curtop = 0;
- if (obj.offsetParent) {
- do {
- curleft += obj.offsetLeft;
- curtop += obj.offsetTop;
- } while (obj = obj.offsetParent);
- }
- return [curleft,curtop];
- }
2. ie中select的innerHtml无效的解决方法
- var oSelect = document.createElement("select");
- var oOption = document.createElement("option");
- var oTextNode=document.createTextNode('test');
- oOption.value=0;
- oOption.appendChild(oTextNode);
- oSelect.appendChild(oOption);
- document.getElementById('yourid').appendChild(oSelect);
直接在dom里面插入一个<script>节点是不行的,可能在本地写程序的时候发现不了。因为直接插入<script>节点,浏览器会下载这个js文件,但是网络有延时,如果你需要在dom里面插入一个js后立即执行某个跟这个js文件有关的函数(一般的程序都会有这个需求的),那么会报错,解决这个问题,方法就是用ajax载入。下面是代码,前面两个函数是别人写的,我加了两个函数,其中callback就是要加载完成js后要执行的函数。
- var lajax={};
- var __count__ = 0;
- lajax.getXmlHttp = function(){
- var xmlHttp = null;
- try {
- xmlHttp = new XMLHttpRequest();
- }
- catch (e) {
- var progIds = ['MSXML2.XMLHTTP', 'Microsoft.XMLHTTP', 'MSXML2.XMLHTTP.5.0'
- , 'MSXML2.XMLHTTP.4.0'
- , 'MSXML2.XMLHTTP.3.0'];
- var success = false;
- for (var iterator = 0; (iterator < progIds.length) && (!success); iterator++) {
- try {
- xmlHttp = new ActiveXObject(progIds[iterator]);
- success = true;
- }
- catch (e) {
- }
- }
- if (!success) {
- return null;
- }
- }
- return xmlHttp;
- };
- lajax.sendRequest = function(method,url,requestData,callBack)
- {
- var xmlHttp = lajax.getXmlHttp();
- xmlHttp.open(method,url,true);
- if (method=="POST") xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
- xmlHttp.onreadystatechange = function()
- {
- if (xmlHttp.readyState == 4) {
- if (xmlHttp.status == 200) {
- callBack(xmlHttp);
- }
- }
- }
- if (method=="POST") xmlHttp.send(requestData);
- else xmlHttp.send(null);
- }
- lajax.loadJS = function(jsPath, callBack) {
- var _callBack = function(xmlHttp) {
- __count__++;
- _includeJS("js" + __count__, xmlHttp.responseText);
- callBack();
- }
- lajax.sendRequest('GET',jsPath, null, _callBack);
- }
- function _includeJS(sId, source) {
- if (( source != null ) && (!document.getElementById(sId))){
- var oHead = document.getElementsByTagName('HEAD').item(0);
- var oScript = document.createElement( "script" );
- oScript.language = "javascript";
- oScript.type = "text/javascript";
- oScript.id = sId;
- oScript.defer = true;
- oScript.text = source;
- oHead.appendChild(oScript);
- }
- }
这个无效的bug已经在ie7中修复,但是毕竟现在用ie6的人还不少,所以还是要需要兼容一下,代码如下:
先是js,当传入的obj为0的时候函数什么都不做,下面有例子:
- <script language="javascript">
- function pointToMenu(obj) {
- if(obj == 0) {
- return;
- }
- obj.className='current';
- }
- function pointOutMenu(obj) {
- if(obj == 0) {
- return;
- }
- obj.className='normal';
- }
- </script>
- <ul class="head-list">
- <li class="current"
- onMouseOver="pointToMenu(0)"
- onMouseOut="pointOutMenu(0)"
- ><a href="index.php"><span>菜单一</span></a></li>
- <li class="normal"
- onMouseOver="pointToMenu(this)"
- onMouseOut="pointOutMenu(this)"
- ><a href="catalog.php?catalog_id=1"><span>菜单二</span></a></li>
- </ul>
css样式表我就省略的要怎么样的样式可以自己定义