javascript - 下面这一段函数是什么意思呀?
阿神
阿神 2017-04-11 11:55:20
[JavaScript讨论组]
var walk_the_DOM = function walk(node, func){
        func(node);
        node = node.firstChild;
        while (node) {
            walk(node,func);
            node = node.nextSibling;
        }
    };
    var getElementsByAttribute = function (att, value) {
        var results = [];
        walk_the_DOM(document.body,function (node) {
            var actual = node.nodeType === 1 && node.getAttribute(att);
            if (typeof actual === "string" && (actual === value || typeof value !== "string")) {
                results.push(node);
            }
        });
        return results;
    };
主要是if (typeof actual === "string" && (actual === value || typeof value !== "string"))这一句


圈住的那一部分

阿神
阿神

闭关修行中......

全部回复(2)
PHP中文网
typeof actual === "string" && (actual === value || typeof value !== "string")

这个“actual ” 是上句获取到的属性值;进而来和value 来做对比;当值一样是也就是

   typeof actual === "string" && (actual === value )

这个条件成立;证明这个是要找的值,这个是找到属性和属性值一样的元素;

typeof actual === "string" && (typeof value !== "string")

这个条件成立时;是找到含有这个属性;但属性值不相同的元素;(从字面上理解)

总结:就是这个通过属性获取元素的方法,必须需要一个属性,属性值非必须;

  当属性值没有传时,返回的时候含有这个属性的元素;
  当属性值传时,返回的是含有属性并且属性值也一样的元素。
伊谢尔伦

条件语句 意思就是 前一段 typeof actual === "string" :=> actual的类型是字符串类型
后面花括号里面 (actual === value || typeof value !== "string") actual严格相等value或者value的类型不是 字符串类型
我不知道 是不是你要的答案

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号