简体   繁体   English

如何在javascript中更改onclick属性?

[英]How do I change onclick attributes in javascript?

I have HTML that displays a button and a paragraph. 我有显示一个按钮和一个段落的HTML。 When you click on the paragraph, it indicates whether the paragraph has been clicked on an odd or even amount of times. 当您单击该段落时,它指示该段落是否被单击了奇数次或偶数次。 I'm trying to get the button to expand the paragraph to include a count of how many times the paragraph has been clicked, and revert back to it's original text when the button is clicked again. 我正在尝试获取按钮来扩展段落,以包括该段落已被单击的次数的计数,并在再次单击该按钮时恢复为原始文本。 For some reason, I don't think my button properly communicates with the paragraph. 由于某些原因,我认为我的按钮无法与段落正确通信。 Here's my code: 这是我的代码:

 var doClickCount = false;
 var clickCounter = 0 
 function toggleParagraphCounter(event) {
     var clickMeEvenTimes = true;
     doClickCount = true;
     if (doClickCount === true) {
        function clickMe() {  
      var para1 = document.getElementById("para1");
      if (clickMeEvenTimes) {
        clickCounter += 1; 
        para1.style = "color:green";
        para1.innerHTML = "Click this paragraph." +
        " It has been clicked an odd number of times.\
        Clicked" + clickCounter + "times with counting enabled." ;
      }
      else {  
        clickCounter += 1;
        para1.style = "font-size:0.9em";
        para1.innerHTML = "Click this paragraph." +
        " It has been clicked an even number of times.\
        Clicked" + clickCounter + "times with counting enabled.";
      }    
      clickMeEvenTimes = !clickMeEvenTimes;
  }
 }  else {
    function clickMe() {  
      var para1 = document.getElementById("para1");
      if (clickMeEvenTimes) {       
        para1.style = "color:green";
        para1.innerHTML = "Click this paragraph." +
        " It has been clicked an odd number of times.";
      }
      else {     
        para1.style = "font-size:0.9em";
        para1.innerHTML = "Click this paragraph." +
        " It has been clicked an even number of times.";
      }    
      clickMeEvenTimes = !clickMeEvenTimes;
  }
 }

 }

What else could I do to try and solve this? 我还能做些什么来解决这个问题?

You are declaring the clickMe function twice, below I have reformatted your code. 您已两次声明clickMe函数,下面已重新格式化您的代码。

 var doClickCount = false;
 var clickCounter = 0 
 function toggleParagraphCounter(event) {

 var clickMeEvenTimes = true;
 doClickCount = true;

 if (doClickCount === true) {

 }  else {

 }
}
function clickMe() {  
  var para1 = document.getElementById("para1");
  if (clickMeEvenTimes) {       
    para1.style = "color:green";
    para1.innerHTML = "Click this paragraph." +
    " It has been clicked an odd number of times.";
  }
  else {     
    para1.style = "font-size:0.9em";
    para1.innerHTML = "Click this paragraph." +
    " It has been clicked an even number of times.";
  }    
  clickMeEvenTimes = !clickMeEvenTimes;
}
function clickMe() {  
  var para1 = document.getElementById("para1");
  if (clickMeEvenTimes) {
    clickCounter += 1; 
    para1.style = "color:green";
    para1.innerHTML = "Click this paragraph." +
    " It has been clicked an odd number of times.\
    Clicked" + clickCounter + "times with counting enabled." ;
  }
  else {  
    clickCounter += 1;
    para1.style = "font-size:0.9em";
    para1.innerHTML = "Click this paragraph." +
    " It has been clicked an even number of times.\
    Clicked" + clickCounter + "times with counting enabled.";
  }    
  clickMeEvenTimes = !clickMeEvenTimes;

} }

Hey so here's the code that worked: 嘿,这是起作用的代码:

 var doClickCount = false;
 var clickCounter = 0 

 function countPar1 () { 
      var para1 = document.getElementById("para1");
      if (clickMeEvenTimes) {
        clickCounter += 1; 
        para1.style = "color:green";
        para1.innerHTML = "Click this paragraph." +
        " It has been clicked an odd number of times.\
        Clicked " + clickCounter + " times with counting enabled." ;
      }
      else {  
        clickCounter += 1;
        para1.style = "font-size:0.9em";
        para1.innerHTML = "Click this paragraph." +
        " It has been clicked an even number of times.\
        Clicked " + clickCounter + " times with counting enabled.";
      }    
      clickMeEvenTimes = !clickMeEvenTimes;
 }

 function toggleParagraphCounter(event) {
 doClickCount =! doClickCount;
     if (doClickCount) {
document.getElementById("para1").onclick = function() {
    countPar1();
};       
 }  else {
 document.getElementById("para1").onclick = function() {
     clickMe();
   };

    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM