在JavaScript中调用函数

时间:2015-04-23 11:15:34

标签: javascript html

我使用HTML button在我的start_KEMT脚本中运行名为recorder.js的函数。



var recording = false;
    var button = document.getElementById('button');

    function start_KEMT() {
       if(!recording)
       {
          init();
          toggleRecording(button);
      }
      toggleRecording(button);
      recording = !recording;
    }


      function toggleRecording(button) {
        var run = parseInt(button.getAttribute('data-run')); 
        
        if(run === 1) {
          recorder && recorder.stop();
          recorder && recorder.exportWAV(function(blob) {
            uploadAudioFromBlob(blob);
          });
          __log('Recording is stopped');
          button.setAttribute('data-run', 0);
          
        } 
          recorder && recorder.clear();
          recorder && recorder.record();
          __log('Speak');
          button.setAttribute('data-run', 1);
        }
      }
      ......

<button type="submit" class="btn btn-info" id="button" onclick="start_KEMT(this)" data-run="0"><i class="fa fa-dot-circle-o fa-fw"></i>&nbsp;</button>
&#13;
&#13;
&#13;

单击按钮后,init()功能运行。 toggleRecording(button)存在问题。我认为这个问题是因为button元素。单击按钮后,控制台中也出现错误:

  

未捕获的TypeError:无法读取属性&#39; getAttribute&#39;为null

你能给我一个完整的例子来修改它吗?我是JS的新手。我只是想修改这个脚本。它不是由我创造的。

2 个答案:

答案 0 :(得分:1)

您正在传递HTML中的button - onclick="start_KEMT(this)"this是按钮元素。但是你没有在函数中定义它。

将您的功能start_KEMT更改为

function start_KEMT(button) {
   if(!recording)
   {
      init();
      toggleRecording(button);
  }
  toggleRecording(button);
  recording = !recording;
}

答案 1 :(得分:0)

<div class="row"> <div id="page-wrapper-lbms" class="small-12 medium-12 large-8 large-offset-2 columns dashboard-page"> <header> <h1>Finalized Deals</h1> </header> <% if @requests != nil @requests.each.with_index do |request,index| if request != nil %> <div class="row"> <div class="small-12 columns"> <div class="deal-item-lbms"> <div class="row"> <div class="small-12 medium-12 large-12 column"> <h4><%= request.title %></h4> </div> </div> <div class="row"> <div class="small-12 medium-12 large-12 column"> <h6>Corresponding Request: Other Request Title</h6> </div> </div> <div class="row"> <div class="small-12 medium-12 large-12 column"> <h6>Corresponding User: Jawad Khawaja</h6> </div> </div> <div class="row"> <div class="small-12 medium-12 large-12 column"> <h6>Meeting Date: 05, Jan 2015</h6> </div> </div> <div class="row"> <div class="small-12 medium-12 large-12 column"> <h6><%= request.created_at %></h6> </div> </div> <input type="hidden" id="b_delete" value="'<%= request.description%>'" name="request_id"/> <div class="deal-btn-grp-lbms"> <button class="tiny" data-reveal-id="contract-details-modal-lbms" onclick="ajax_finalized('<%= request.description %>')">View Contract Details</button> <button class="tiny">Delete</button> </div> </div> <hr /> </div> </div> <%end%> <%end%> <%end%> </div> </div> <div id="contract-details-modal-lbms" class="reveal-modal" data-reveal> <h3>Contract Details</h3> <p id="contract_details"></p> </div> <script> function ajax_finalized(abc){ $('#contract_details').innerHTML = 'abc'; alert("yo!"); } </script> 中,将您的功能签名从recorder.js更改为start_KEMT()。这将解决您的问题,因为它(即按钮变量)现在未定义。