如何在加载页面时自动触发按钮

时间:2015-07-08 09:28:14

标签: javascript jquery html events click

我已经包含了给定的代码

<div class="tab-control">
  <ul class="tabs">
    <li class="active">
      <a href="#aggregated">Aggregated</a>
    </li>
    <li>
      <a href="#app">App</a>
    </li>
    <li>
      <a href="#operator">Operator</a>
    </li>
    <li>
      <a href="#top">Top<br>by User</a>
    </li>
    <li>
      <a href="#total">Total</a>
    </li>
    <li>
      <a href="#session">Sessions</a>
    </li>
  </ul>
  <div class="frames">
    <div class="frame" id="app">
      welcome app
      <div class="time">yola</div>
    </div>
    <div class="frame" id="operator">
      welcome operator
      <div class="time">yola</div>
    </div>
    <div class="frame" id="aggregated">
      welcome aggregated
      <div class="time">yola</div>
    </div>
    <div class="frame" id="top">
      Thankyou top
      <div class="time">yola</div>
    </div>
    <div class="frame" id="total">
      Exact Total
      <div class="time">yola</div>
    </div>
    <div class="frame" id="session">
      Your session
      <div class="time">yola</div>
    </div>
  </div>
</div>

在我的js中我写了这个

$(document).ready(function() {
$('.tab-control').tabcontrol().bind("tabcontrolchange", function(event, frame) {
  switch($(frame).attr('id')) {
    case "operator":
      break;
    case "aggregated":
      $('.time').text('aggregates')
      break;
    case "app":
      $('.time').text('app')
      break;
    case "top":
      $('.time').text('top')
      break;
    case "total":
      $('.time').text('total')
      break;
    case "session":
      $('.time').text('session')
      break;
  }
});
})

但是当页面加载时,它显示聚合标签处于活动状态并显示 <div class="time">yola</div>当我点击此标签时,它会变为 <div class="time">aggregates</div>所以当我的页面加载时我需要它自动点击标签。请不要给我我在<div class="time">aggregates</div>中设置的答案,因为我的要求不同。提前致谢

2 个答案:

答案 0 :(得分:2)

根据我的理解,您想点击第一个标签列表项链接?

如果是这样的话,那么:

$(document).ready(function() {
  $('.tabs a').first().click();
});

答案 1 :(得分:0)

如果我理解正确,你想要的是第一次在document.ready上运行click事件而不需要任何点击。

我建议你命名你的功能并重新调用两次

$(document).ready(NameofFunction(event, $('.frame #aggregated') ));
$(#yourButton).on("click", NameofFunction(event, frame));

... mmmmaybe? ^^