2013-01-06 36 views
1

我做了一個類,以便可以根據元素id,類,名稱或href中的關鍵字查找特定元素。如果找到匹配,它將被推送到一個數組。我設法讓它自己工作,但是當我將它變成一個類時,它不會將任何元素推送到數組中。它似乎在一定程度上工作,因爲它找到所有的a元素,但它只是不匹配關鍵字。這裏似乎有什麼錯誤?JavaScript類根據ID,類,href或名稱中的關鍵字找到元素

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); 
+0

IM檢查出來... – sajawikio

+1

請創建http://jsfiddle.net/演示。 –

回答

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

+0

啊!對,它的功能哈哈。感謝您查看它。 –

+0

MAN很好,我發現我的upvoted太出色了。 – sajawikio

+0

快樂編碼兄弟... :) – Wolf

相關問題