单击后显示“未定义”的按钮

时间:2020-06-23 00:58:53

标签: javascript button undefined innerhtml

我有一段非常基本的代码,但是在代码运行后,按钮变为未定义状态,如何停止它呢?

<button id="buttonevent" onclick="partyTime()">What time is it?</button>

<script>

function partyTime() {
    document.getElementById("buttonevent").innerHTML=alert("It's Party Time!");
}

</script>

1 个答案:

答案 0 :(得分:0)

button不返回任何内容,因此您将undefiened的内容设置为alert。只需输入innerHTML代码,而无需设置按钮。<button id="buttonevent" onclick="partyTime()">What time is it?</button> <script> function partyTime() { alert("It's Party Time!"); } </script>

onclick

而且,这是您的新手,让我们从一开始就养成一个坏习惯。尽管您可能会在HTML中看到许多使用<button id="buttonevent">What time is it?</button> <script> // Set up the click of the button to call the partyTime function here // in JavaScript, not from the HTML. document.getElementById("buttonevent").addEventListener("click", partyTime); function partyTime() { alert("It's Party Time!"); } </script>的代码,但是您不应以此方式设置事件。这是一种25岁以上的技术,不会死掉它应得的for so many reasons。而是将JavaScript与HTML分开,并在JavaScript中进行事件绑定。

ref.df <- data.frame(variable = c('a', 'b', 'c'),
                     operation = c('mean', 'sum', 'length'), stringsAsFactors = F)

full.df <- data.frame(a = rnorm(20),
                      b = rnorm(20),
                      c = rnorm(20))
  
  
for(r in 1:nrow(ref.df)){
    
    rowRef <- ref.df[r, ]
    
    rowRef
    # variable operation
    #  a      mean
    
    # this says in the full.df, take the mean of `a`, or sum `b` or 
    # take the length of `c`. I tried this
    
    rowRef$operation(full.df[rowRef$variable])
    
    # Error: attempt to apply non-function 
  }