javascript类根据id,class,href或name中的关键字查找元素

时间:2013-01-06 15:33:19

标签: javascript

我创建了一个类,以便我可以根据元素id,class,name或href中的关键字找到特定元素。如果找到匹配,则将其推送到数组。我设法让它自己工作,但当我把它变成一个类时,它不会将任何元素推送到数组。它似乎在某种程度上起作用,因为它找到了所有a元素,但它与关键字不匹配。这里看来有什么不对?

HTML

<a href="linka.html" id="poll-answer-8234" class="ca" name="poll_430" value="8234">
<a href="linka.html" id="poll-answer-9234" class="ca" name="poll_430" value="9234">  
<a href="linkb.html" id="survey-answer-7866" class="cb" name="poll_430" value="7866">
<a href="linkb.html" id="survey-answer-8998" class="cb" name="poll_430" value="8998">
<a href="linkc.html" id="quiz-answer-7866" class="cc" name="poll_430" value="7866">
<a href="linkc.html" id="quiz-answer-8998" class="cc" name="poll_430" value="8998">

JS

function nodeBulder(parameters) {
    this.selecter = parameters.selecter;
    this.targets = [];
    this.nodes = document.getElementsByTagName(parameters.tag);
    this.keyword = parameters.keyword;
    this.build = function () {
        switch (this.selecter) {
            case "byid":
                var count, node;
                for (count = 0; count < this.nodes.length; count++) {
                    node = this.nodes[count].id.indexOf(this.keyword);
          console.log(node);
                    if (node === 0) {
                        this.targets.push(this.nodes[count]);
                    }
                }
                break;
            case "byname":
                var count, node;
                for (count = 0; count < this.nodes.length; count++) {
                    node = this.nodes[count].name.indexOf(this.keyword);
                    if (node === 0) {
                        this.targets.push(this.nodes[count]);
                    }
                }
                break;
            case "byclass":
                var count, node;
                for (count = 0; count < this.nodes.length; count++) {
                    node = this.nodes[count].className.indexOf(this.keyword);
                    if (node === 0) {
                        this.targets.push(this.nodes[count]);
                    }
                }
                break;
            case "byhref":
                var count, node;
                for (count = 0; count < this.nodes.length; count++) {
                    node = this.nodes[count].href.indexOf(this.keyword);
                    if (node === 0) {
                        this.targets.push(this.nodes[count]);
                    }
                }
                break;
        }
    }
    this.random = function () {
        return this.targets[Math.floor(Math.random() * this.targets.length)];
    }
}

var idTest = new nodeBulder({ selecter: "byid", tag: 'a', keyword: 'poll-answer' });
var classTest = new nodeBulder({ selecter: "byclass", tag: 'a', keyword: 'cc' });

console.log(idTest.targets);
console.log(classTest.targets);

1 个答案:

答案 0 :(得分:2)

问题是,您没有调用构建方法。

function nodeBulder(parameters) {
    this.selecter = parameters.selecter;
    this.targets = [];
    this.nodes = document.getElementsByTagName(parameters.tag);
    this.keyword = parameters.keyword;
    this.build = function () {
        switch (this.selecter) {
            case "byid":
                var count, node;
                for (count = 0; count < this.nodes.length; count++) {
                    node = this.nodes[count].id.indexOf(this.keyword);
          console.log(node);
                    if (node === 0) {
                        this.targets.push(this.nodes[count]);
                    }
                }
                break;
            case "byname":
                var count, node;
                for (count = 0; count < this.nodes.length; count++) {
                    node = this.nodes[count].name.indexOf(this.keyword);
                    if (node === 0) {
                        this.targets.push(this.nodes[count]);
                    }
                }
                break;
            case "byclass":
                var count, node;
                for (count = 0; count < this.nodes.length; count++) {
                    node = this.nodes[count].className.indexOf(this.keyword);
                    if (node === 0) {
                        this.targets.push(this.nodes[count]);
                    }
                }
                break;
            case "byhref":
                var count, node;
                for (count = 0; count < this.nodes.length; count++) {
                    node = this.nodes[count].href.indexOf(this.keyword);
                    if (node === 0) {
                        this.targets.push(this.nodes[count]);
                    }
                }
                break;
        }
    }
    this.random = function () {
        return this.targets[Math.floor(Math.random() * this.targets.length)];
    }
}

var idTest = new nodeBulder({ selecter: "byid", tag: 'a', keyword: 'poll-answer' });
idTest.build();
var classTest = new nodeBulder({ selecter: "byclass", tag: 'a', keyword: 'cc' });
classTest.build();

console.log(idTest.targets);
console.log(classTest.targets);

检查demo here