简体   繁体   English

在jQuery中添加点击页面加载

[英]adding click on page load in jquery

I have two tabs. 我有两个标签。 there is HTML assigned to both the tabs.when i open the page i want one of the tab to be clicked automatically. 这两个选项卡都分配了HTML。当我打开页面时,我希望自动单击其中一个选项卡。

My front page is coming empty.when I click on one of the tab then only it is showing the data. 我的首页即将变空。当我单击一个选项卡时,只有它显示数据。 i want one of the tab to be shown on page opening. 我希望在打开页面时显示其中一个标签。 My Code: 我的代码:

HTML 的HTML

<div class="main">
        <ul>
            <li h><a  id="devices">devices</a></li>
            <li><a id= "tags">tags</a></li>
        </ul>
    </div>

Jquery jQuery的

$#devices).trigger('click');
$("#devices").click(function(){
    //some code
});
$("#tags").click(function(){
    //some code
});

You can use click trigger, but you must use it after .click event is declared: 您可以使用click触发器,但是必须在声明.click事件之后使用它:

// your click event declaration
$("#devices").click(function(){
    //some code
});

// trigger    
$("#devices").trigger("click");
$("#devices").click(function () {
    //some code
});
$("#tags").click(function () {
    //some code
});
$(function () {
    $("#devices").click();
});

fiddle 小提琴

Try like this, 这样尝试

$(function(){

        $("#devices").click(function(){
              //some code
        });

        $("#tags").click(function(){
              //some code
        });

        $("#devices").trigger('click');

});
       <div class="main">
            <ul>
                <li><a id="devices">devices</a></li>
                <li><a id= "tags">tags</a></li>
            </ul>
        </div>

    <script>
    $("document").ready(function()
    {
        $("#devices").click(function(){alert("A")})

        $("#tags").click(function(){alert("A1")})

        $("#devices").trigger('click');
    }); 
    </script>    
<ul>
  <li><a href='#tab1'>Link1</a></li>
  <li><a href='#tab2'>Link2</a></li>
<ul>

<script>
    $(window).load(function () {
        $('#tab1').click();
        });
</script>

or more generic,using hash tags,In below code if url has #code then it will automatically click 2nd tab eg: www.text.com/index.html#tab2

$(window).load(function () {
if(window.location.hash!="" && window.location.hash=='#tab2'){
   $('#tab1').click();
}else {
   $('#tab2').click();
}
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM