【问题标题】:Jquery button 'click()' function not working to add new html textJquery按钮'click()'功能无法添加新的html文本
【发布时间】:2015-11-24 01:32:20
【问题描述】:

我试图弄清楚为什么这个函数在被调用时没有执行

 $(document).ready(function() : 

function sayQuote() { 
    $('#button').click($('#quotebox').html(function() { 
        var getQuote = quotes[randomNum];
        return getQuote;      
    });
    );
}

$(document).ready(function() {
    sayQuote();
});

控制台说我的参数后缺少一个')',但我无法确定在哪里需要另一个。

【问题讨论】:

    标签: javascript jquery html button click


    【解决方案1】:

    将您的代码更改为:

     function sayQuote() 
     { 
          $('#button').click(function(){
               $('#quotebox').html(function(){ 
                    var getQuote = quotes[randomNum];
                    return getQuote;      
               });
          });
     }
    

    【讨论】:

      【解决方案2】:

      我认为您不必将按钮单击处理程序放在可能是这样的函数中

      $('#button').click(function(){
          $('#quotebox').html(quotes[randomNum]);
      }); 
      

      如果你想通过代码调用这个事件,你可以使用

      $("#button").click();
      

      【讨论】:

        【解决方案3】:

        没有必要让事情变得过于复杂。

        function sayQuote() {
            $('#button').click(function() {
                var getQuote = quotes[randomNum];
                $('#quotebox').html(getQuote);
            });
        }
        
        $(document).ready(sayQuote);
        

        【讨论】:

          【解决方案4】:

          您正在尝试在 jquery 的 .html() 方法中定义一个函数,这没有意义实现解决方案的正确方法将是这样的。

          function sayQuote() { 
          $('#button').click(function(){
            var getQuote = quotes[random];
            $('#quotebox').html(getQuote);
          });
          }
          $(document).ready(function() {
              sayQuote();
          });
          

          【讨论】:

            猜你喜欢
            • 2023-03-15
            • 2016-03-29
            • 2013-11-02
            • 1970-01-01
            • 2010-12-01
            • 2013-08-13
            • 1970-01-01
            • 1970-01-01
            • 2015-02-07
            相关资源
            最近更新 更多