Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript .onclick按钮未执行功能_Javascript_Html_Function_Onclick - Fatal编程技术网

Javascript .onclick按钮未执行功能

Javascript .onclick按钮未执行功能,javascript,html,function,onclick,Javascript,Html,Function,Onclick,我的.onclick函数有问题,无法执行脚本文件中指定的函数。这是我的密码: JAVASCRIPT: var quotes = [ ["“If you are distressed by anything external, the pain is not due to the thing itself, but to your estimate of it; and this you have the power to revoke at any moment.”", "Marcus

我的.onclick函数有问题,无法执行脚本文件中指定的函数。这是我的密码:

JAVASCRIPT:

var quotes = [
    ["“If you are distressed by anything external, the pain is not due to the thing itself, but to your estimate of it; and this you have the power to revoke at any moment.”", "Marcus Aurelius"],["“The average man is a conformist, accepting miseries and disasters with the stoicism of a cow standing in the rain.”", "Colin Wilson"],
    ["“Never let the future disturb you. You will meet it, if you have to, with the same weapons of reason which today arm you against the present.”", "Marcus Aurelius"],
    ["“Warriors should suffer their pain silently.”","Erin Hunter"],
    ["“Complaining does not work as a strategy. We all have finite time and energy. Any time we spend whining is unlikely to help us achieve our goals. And it won't make us happier.”", "Randy Pausch"],
    ["“It is the power of the mind to be unconquerable.”", "Seneca"],
    ["“How do you defeat terrorism? Don’t be terrorized.”", "Salman Rushdie"],
    ["“It is not the man who has too little that is poor, but the one who hankers after more.”", "Seneca"],
    ["“What really frightens and dismays us is not external events themselves, but the way in which we think about them. It is not things that disturb us, but our interpretation of their significance.”", "Epictetus"],
    ["“For death remembered should be like a mirror, who tells us life’s but breath, to trust it error.”", "Shakespeare"],
    ["“No one saves us but ourselves. No one can and no one may. We ourselves must walk the path.”", "Buddha"],
    ["“You only lose what you cling to.”", "Buddha"],
    ["“Man suffers only because he takes seriously what the gods made for fun.”", "Alan W. Watts"],
    ["“If there is any religion that could respond to the needs of modern science, it would be Buddhism.”", "Albert Einstein"],
    ["“We are not going in circles, we are going upwards. The path is a spiral; we have already climbed many steps.”", "Hermann Hesse"],
    ["“Treat every moment as your last. It is not preparation for something else.”", "Shunryu Suzuki"],
    ["“Better than a thousand hollow words is one word that brings peace.”", "Buddha"],
    ["“Life is painful. It has thorns, like the stem of a rose. Culture and art are the roses that bloom on the stem. The flower is yourself, your humanity. Art is the liberation of the humanity inside yourself.”", "Daisaku Ikeda"],
    ["“Learning to let go should be learned before learning to get. Life should be touched, not strangled. You’ve got to relax, let it happen at times, and at others move forward with it.”", "Ray Bradbury"]  
];


var randomInt =  Math.floor((Math.random() * quotes.length) + 1);

// Generate random number
// Get number's position from array 
var aQuote = function() {
    return quotes[randomInt][0] + "";
};

var aQuoteAuthor = function() {
    return quotes[randomInt][1] + "";
};


// Change textblock into quote + author
// Display quote on page
// Display author on page

function changeButton() {
    document.getElementById('quote').innerHTML = aQuote();
    document.getElementById('quoteAuthor').innerHTML = aQuoteAuthor();
};

// Register button press 
document.getElementById('aButton').onclick = changeButton();
这是我的html:

  <div class="container">
    <div class="row">
      <div class="u-full-width">
        <div class="quotediv">
            <h5 id="quote" class="text-primary-color">“Imagine smiling after a slap in the face. Then think of doing it twenty-four hours a day.”</h5>
            <p id="quoteAuthor" class="light-primary-color">Markus Zusak<p>
            <button id="aButton" type="button button-primary" class="primary-text-color"> NEW QUOTE</button>
        </div>
      </div>
    </div>
  </div>

  <script src ="scripts/script.js"></script>

“想象一下打了一记耳光后的微笑。然后想想每天二十四小时都这样做。”
马库斯祖萨
新报价
第一件奇怪的事情是,函数changeButton()在页面加载时自动调用,而无需函数调用。因此,在每次页面加载时,都会生成并显示一个新的报价。第二件奇怪的事情是按钮本身没有通过函数changeButton()生成和更改引号

谁能给我指一下正确的方向吗


顺便说一句,我是个编程新手。这是我的第一个问题。我试着按照指导原则进行具体说明,但如果您对我的问题表述有任何提示或意见,请让我知道!:)

问题在于您正在执行函数并将返回值分配给onclick

document.getElementById('aButton').onclick = changeButton();
您应该像这样将函数本身分配给onclick(注意它的末尾没有“()”)

这样使用更好


您可以使用调用
onClick
侦听器

document.getElementById('aButton').addEventListener("click", changeButton);
使用:

document.getElementById('aButton').onclick = changeButton();
您只需指定函数的返回值,而不是将函数指定给单击事件!这一行:

document.getElementById('aButton').onclick = changeButton;
实际将函数分配给单击事件(注意,没有括号,因此它不会执行,它被分配为变量)

函数更改按钮(){
警报(“你好”)
}
document.getElementById('aButton')。addEventListener(“单击”,更改按钮)

单击我
尝试这样做:

document.getElementById('aButton')。onclick=changeButton

您所做的是绑定事件并同时执行它

document.getElementById('aButton')。onclick=changeButton
函数changeButton(){
控制台日志(“是”);
}

按钮

将脚本移动到标题部分,这样就可以正常工作了

<!DOCTYPE html>
<html>
<head>

<script>
var quotes = [
    ["“If you are distressed by anything external, the pain is not due to the thing itself, but to your estimate of it; and this you have the power to revoke at any moment.”", "Marcus Aurelius"],["“The average man is a conformist, accepting miseries and disasters with the stoicism of a cow standing in the rain.”", "Colin Wilson"],
    ["“Never let the future disturb you. You will meet it, if you have to, with the same weapons of reason which today arm you against the present.”", "Marcus Aurelius"],
    ["“Warriors should suffer their pain silently.”","Erin Hunter"],
    ["“Complaining does not work as a strategy. We all have finite time and energy. Any time we spend whining is unlikely to help us achieve our goals. And it won't make us happier.”", "Randy Pausch"],
    ["“It is the power of the mind to be unconquerable.”", "Seneca"],
    ["“How do you defeat terrorism? Don’t be terrorized.”", "Salman Rushdie"],
    ["“It is not the man who has too little that is poor, but the one who hankers after more.”", "Seneca"],
    ["“What really frightens and dismays us is not external events themselves, but the way in which we think about them. It is not things that disturb us, but our interpretation of their significance.”", "Epictetus"],
    ["“For death remembered should be like a mirror, who tells us life’s but breath, to trust it error.”", "Shakespeare"],
    ["“No one saves us but ourselves. No one can and no one may. We ourselves must walk the path.”", "Buddha"],
    ["“You only lose what you cling to.”", "Buddha"],
    ["“Man suffers only because he takes seriously what the gods made for fun.”", "Alan W. Watts"],
    ["“If there is any religion that could respond to the needs of modern science, it would be Buddhism.”", "Albert Einstein"],
    ["“We are not going in circles, we are going upwards. The path is a spiral; we have already climbed many steps.”", "Hermann Hesse"],
    ["“Treat every moment as your last. It is not preparation for something else.”", "Shunryu Suzuki"],
    ["“Better than a thousand hollow words is one word that brings peace.”", "Buddha"],
    ["“Life is painful. It has thorns, like the stem of a rose. Culture and art are the roses that bloom on the stem. The flower is yourself, your humanity. Art is the liberation of the humanity inside yourself.”", "Daisaku Ikeda"],
    ["“Learning to let go should be learned before learning to get. Life should be touched, not strangled. You’ve got to relax, let it happen at times, and at others move forward with it.”", "Ray Bradbury"]  
];


var randomInt =  Math.floor((Math.random() * quotes.length) + 1);

// Generate random number
// Get number's position from array 
var aQuote = function() {
    return quotes[randomInt][0] + "";
};

var aQuoteAuthor = function() {
    return quotes[randomInt][1] + "";
};


// Change textblock into quote + author
// Display quote on page
// Display author on page

function changeButton() {
    alert();
    document.getElementById('quote').innerHTML = aQuote();
    document.getElementById('quoteAuthor').innerHTML = aQuoteAuthor();
};

</script>
</head>
<body>

  <div class="container">
    <div class="row">
      <div class="u-full-width">
        <div class="quotediv">
            <h5 id="quote" class="text-primary-color">“Imagine smiling after a slap in the face. Then think of doing it twenty-four hours a day.”</h5>
            <p id="quoteAuthor" class="light-primary-color">Markus Zusak<p>
            <button id="aButton" type="button button-primary"  class="primary-text-color"> NEW QUOTE</button>
        </div>
      </div>
    </div>
  </div>

</body>
</html> 

变量引号=[
[“如果你被任何外部事物所困扰,痛苦不是因为事物本身,而是因为你对它的估计;这一点你有权随时撤销。”,“马库斯·奥雷利乌斯”],[“普通人是一个墨守成规的人,像站在雨中的奶牛一样坚忍地接受痛苦和灾难。”,“科林·威尔逊”],
[“永远不要让未来打扰你。如果你必须的话,你会用今天武装你对抗现在的理性武器来迎接它。”,“马库斯·奥雷利乌斯”],
[“战士们应该默默忍受痛苦。”,“艾琳·亨特”],
[“抱怨并不是一种策略。我们都有有限的时间和精力。我们花在抱怨上的任何时间都不可能帮助我们实现目标。它也不会让我们更快乐。”,“Randy Pausch”,
[“不可征服是心灵的力量。”,“塞内卡”],
[“你如何打败恐怖主义?不要被恐吓。”,“萨尔曼·拉什迪”],
[“穷人不是拥有太少的人,而是渴望得到更多的人。”,“塞内卡”],
[“真正让我们害怕和沮丧的不是外部事件本身,而是我们思考它们的方式。不是事物打扰了我们,而是我们对它们的意义的解释。”,“Epictetus”,
[“记住的死亡应该像一面镜子,告诉我们生命的气息,相信它是错误的。”,“莎士比亚”,
[“除了我们自己,没有人能救我们。没有人能,也没有人可以。我们自己必须走这条路。”,“佛陀”,
[“你只会失去你所坚持的东西。”,“佛”,
[“人类之所以遭受痛苦,仅仅是因为他认真对待神为娱乐而创造的东西。”,“艾伦·W·瓦茨”,
[“如果有任何宗教能够满足现代科学的需要,那就是佛教。”,“阿尔伯特·爱因斯坦”],
[“我们不是在兜圈子,我们在往上走。这条路是螺旋形的;我们已经爬了很多台阶。”,“赫尔曼·黑塞”],
[“把每一刻都当作你的最后一刻。这不是为其他事情做准备。”,“铃木俊良”],
[“一个字带来和平,胜过千言万语。”,“佛”],
[“生活是痛苦的。它有刺,就像玫瑰的茎。文化和艺术是在茎上绽放的玫瑰。花是你自己,你的人性。艺术是你内心人性的解放。”,“池田大作”],
[“学会放手应该在学会获得之前学会。生活应该被触动,而不是被扼杀。你必须放松,让它不时发生,在其他人身上继续前进。”,“Ray Bradbury”]
];
var randomInt=Math.floor((Math.random()*quotes.length)+1);
//生成随机数
//从数组中获取数字的位置
var aqueote=函数(){
返回引号[randomInt][0]+“”;
};
var aqueoteauthor=函数(){
返回引号[randomInt][1]+“”;
};
//将textblock更改为quote+author
//在页面上显示报价
//在页面上显示作者
函数changeButton(){
警惕();
document.getElementById('quote')。innerHTML=aQuote();
document.getElementById('quoteAuthor')。innerHTML=aqoteAuthor();
};
“想象一下打了一记耳光后的微笑。然后想想每天二十四小时都这样做。”
马库斯祖萨
新报价

谁在调用
changeButton
function如何将事件侦听器附加到元素
document.getElementById('aButton')。addEventListener('click',changeButton())试试这个
document.getElementById('#id')。在('click',changebutton())上看起来像document.getElementById('aButton')。onclick=changeButton();正在引发问题。如果你只是想
document.getElementById('aButton').onclick = changeButton;
<!DOCTYPE html>
<html>
<head>

<script>
var quotes = [
    ["“If you are distressed by anything external, the pain is not due to the thing itself, but to your estimate of it; and this you have the power to revoke at any moment.”", "Marcus Aurelius"],["“The average man is a conformist, accepting miseries and disasters with the stoicism of a cow standing in the rain.”", "Colin Wilson"],
    ["“Never let the future disturb you. You will meet it, if you have to, with the same weapons of reason which today arm you against the present.”", "Marcus Aurelius"],
    ["“Warriors should suffer their pain silently.”","Erin Hunter"],
    ["“Complaining does not work as a strategy. We all have finite time and energy. Any time we spend whining is unlikely to help us achieve our goals. And it won't make us happier.”", "Randy Pausch"],
    ["“It is the power of the mind to be unconquerable.”", "Seneca"],
    ["“How do you defeat terrorism? Don’t be terrorized.”", "Salman Rushdie"],
    ["“It is not the man who has too little that is poor, but the one who hankers after more.”", "Seneca"],
    ["“What really frightens and dismays us is not external events themselves, but the way in which we think about them. It is not things that disturb us, but our interpretation of their significance.”", "Epictetus"],
    ["“For death remembered should be like a mirror, who tells us life’s but breath, to trust it error.”", "Shakespeare"],
    ["“No one saves us but ourselves. No one can and no one may. We ourselves must walk the path.”", "Buddha"],
    ["“You only lose what you cling to.”", "Buddha"],
    ["“Man suffers only because he takes seriously what the gods made for fun.”", "Alan W. Watts"],
    ["“If there is any religion that could respond to the needs of modern science, it would be Buddhism.”", "Albert Einstein"],
    ["“We are not going in circles, we are going upwards. The path is a spiral; we have already climbed many steps.”", "Hermann Hesse"],
    ["“Treat every moment as your last. It is not preparation for something else.”", "Shunryu Suzuki"],
    ["“Better than a thousand hollow words is one word that brings peace.”", "Buddha"],
    ["“Life is painful. It has thorns, like the stem of a rose. Culture and art are the roses that bloom on the stem. The flower is yourself, your humanity. Art is the liberation of the humanity inside yourself.”", "Daisaku Ikeda"],
    ["“Learning to let go should be learned before learning to get. Life should be touched, not strangled. You’ve got to relax, let it happen at times, and at others move forward with it.”", "Ray Bradbury"]  
];


var randomInt =  Math.floor((Math.random() * quotes.length) + 1);

// Generate random number
// Get number's position from array 
var aQuote = function() {
    return quotes[randomInt][0] + "";
};

var aQuoteAuthor = function() {
    return quotes[randomInt][1] + "";
};


// Change textblock into quote + author
// Display quote on page
// Display author on page

function changeButton() {
    alert();
    document.getElementById('quote').innerHTML = aQuote();
    document.getElementById('quoteAuthor').innerHTML = aQuoteAuthor();
};

</script>
</head>
<body>

  <div class="container">
    <div class="row">
      <div class="u-full-width">
        <div class="quotediv">
            <h5 id="quote" class="text-primary-color">“Imagine smiling after a slap in the face. Then think of doing it twenty-four hours a day.”</h5>
            <p id="quoteAuthor" class="light-primary-color">Markus Zusak<p>
            <button id="aButton" type="button button-primary"  class="primary-text-color"> NEW QUOTE</button>
        </div>
      </div>
    </div>
  </div>

</body>
</html>