Dakular's profile打哭了的拨烙锅PhotosBlogLists Tools Help

Blog


    3/14/2007

    [备忘]getElementsByClassName 和js取地址栏参数

    getElementsByClassName()
      为了从一大堆HTML代码中找出我们的树状菜单(也许有多个),我们先来实现一个通
    过className找DOM节点的方法:getElementsByClassName。这是对浏览器自有DOM方法
    的一个简单但实用的扩充。

      此方法有两个参数:ele指出以哪个DOM节点为根节点寻找(也就是说只找ele的子
    节点),className指出符合条件的节点的class属性中必须包含怎样的className。它
    的返回值是一个数组,存放了所有符合条件的节点。

    function getElementsByClassName(ele,className) {
    //获取所有子节点
    if(document.all){
    var children = ele.all;
    }else{
    var children = ele.getElementsByTagName('*');
    }
    //遍历子节点并检查className属性
    var elements = new Array();
    for (var i = 0; i < children.length; i++) {
    var child = children[i];
    var classNames = child.className.split(' ');
    for (var j = 0; j < classNames.length; j++) {
    if (classNames[j] == className) {
    elements[elements.length] = child;
    break;
    }
    }
    }
    return elements;
    }

    var trees = getElementsByClassName(document,'TreeView');

      最前面的一个if-else语是为了兼容IE5(IE5不能运行
    document.getElementsByTagName('*'))。需要注意的是千万不要用浏览器检测的方法
    来写脚本,而应该直接使用将要用到的语句来测试是否可以执行,如果返回值为null或
    undefined,那再换一种方法。这样的脚本可以有更好的兼容性,也更健壮。

      elements[elements.length] = child;,这句同样是为了兼容IE5才没有使用数组
    的push方法。如果你一定要使用push方法,那么可以在执行getElementsByClassName()
    之前先重载一遍push方法。代码如下:

    Array.prototype.push = function(value){
    this[this.length] = value;
    }  注:原本我希望getElementsByClassName也能像push方法一样写,比如
    HTMLElement.prototype.getElementsByClassName = ...。不过实际操作的时候发现在
    运行时HTMLElement这个对象并不是固定的,每种tag似乎都不一样,只能作罢。

    ========================================================================

    取地址栏参数

    //v1:
    var URLParams = new Array();
    var aParams = document.location.search.substr(1).split('&');
    for (i=0; i < aParams.length i++){
    var aParam = aParams.split('=');
    URLParams[aParam[0]] = aParam[1];
    }
    //取得传过来的name参数
    name=URLParams['name'];

    //v2:
    Request = {
    QueryString : function(item){
    var svalue = location.search.match(new
    RegExp('[\?\&]' + item + '=([^\&]*)(\&?)','i'));
    return svalue ? svalue[1] : svalue;
    }
    }
    var key = Request.QueryString('key');

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://dakular.spaces.live.com/blog/cns!8668FFEC49BC51CE!477.trak
    Weblogs that reference this entry
    • None