2015-10-15 73 views
0

所以基本上这个代码使文本在按钮上,但我希望它显示在按钮下面,所以每次我点击按钮上的文本将更新为以下几点:https://jsfiddle.net/santon/mc15vy4f/ < -----例如我想要这个java脚本来显示下面的按钮不在按钮上的答案,请看看

var myBtn = document.getElementById("myButton"); 

var clickTracker = { 
     count:0, 
    getMessage: function() { 
    var message; 

    switch(this.count) { 
     case 1: message = "You pushed the button"; break; 
     case 2: message = "You pushed the button (again)."; break; 
     case 3: //fall Through 
     case 4: //fall Through 
     case 5: //fall Through 
      message = "You pushed the button "+ this.count + " times."; break; 
     default: message = "Stop pushing the button"  
    } 
    return message; 
    } 
}; 


function processClick() { 
    clickTracker.count++; 
     this.innerHTML = clickTracker.getMessage(); 
} 

myBtn.addEventListener("click", processClick); 
+1

那就这样做。目前,您正在将文本分配给按钮。您使用不同元素的问题是什么?如果我们不知道您的问题是什么,我们无法联系到您。 –

+0

即时通讯不太确定如何做到这一点 –

+1

你一定已经想过* * *。你是否完全熟悉DOM API?最简单的解决方案可能是在按钮之后添加一个带有一些ID的新元素,并添加到'document.getElementByID('someID')'而不是'this'。 –

回答

0

在这一行:

this.innerHTML = clickTracker.getMessage();

你告诉按钮来改变它的内部HTML。 如果你想为text/html的按钮后出现,你可以做到以下几点:

var myBtn = document.getElementById("myButton"); 
var textContainer; 

var clickTracker = { 
     count:0, 
    getMessage: function() { 
    var message; 

    switch(this.count) { 
     case 1: message = "You pushed the button"; break; 
     case 2: message = "You pushed the button (again)."; break; 
     case 3: //fall Through 
     case 4: //fall Through 
     case 5: //fall Through 
      message = "You pushed the button "+ this.count + " times."; break; 
     default: message = "Stop pushing the button"  
    } 
    return message; 
    } 
}; 


function processClick() { 
    clickTracker.count++; 
     var text = clickTracker.getMessage(); 
     if(!textContainer){ //checking if container is not yet set 
      textContainer = document.createElement("div"); //creating the element 
      this.parentElement.appendChild(textContainer);//appending to parent of button 
     } 
     textContainer.textContent = text; 
} 

myBtn.addEventListener("click", processClick); 
+0

它不工作的原因,你可能会张贴在这个链接,并尝试运行它https://jsfiddle.net/santon/mc15vy4f/ –

+0

完美作品:https://jsfiddle.net/mc15vy4f/7/ – Saar

0

更改HTML这样的:

<button id="myButton">Click Me</button> 
<p id="stat"></p> 

JS代码:

var myBtn = document.getElementById("myButton"); 

var clickTracker = { 
    count:0, 
    getMessage: function() { 
     var message; 

     switch(this.count) { 
      case 1: message = "You pushed the button"; break; 
      case 2: message = "You pushed the button (again)."; break; 
      case 3: //fall Through 
      case 4: //fall Through 
      case 5: //fall Through 
       message = "You pushed the button "+ this.count + " times."; break; 
      default: message = "Stop pushing the button"  
     } 
     return message; 
    } 
}; 


function processClick() { 
    clickTracker.count++; 
    document.getElementById("stat").innerHTML = clickTracker.getMessage(); 
} 

myBtn.addEventListener("click", processClick); 
相关问题