Jquery how to trigger click?

2019-09-05 09:54发布

问题:

<?php if(isset($_SESSION['cnt'])&&($_SESSION['cnt'] == 5) ){  
$_SESSION['cnt'] = 0;
?>
    <script> 
            //$("#fb_login").trigger('click');
    //$('#fb_login').click();
    $("#fb_login").click(function(){ 
        $("#fb_login").colorbox({width:"300px", height:"200px", inline:true, href:"#loginask_iframe"});
    });
    </script>
<?php } ?>
<a href="#" id="fb_login"></a>
<div style='display:none' class="loginask_iframe_container">
<div id="loginask_iframe">
test
</div>
</div>

i can't get the click function to trigger when the $_SESSION['cnt'] == 5.

any ideas what i am doing wrong? thanks

回答1:

<script type="text/javascript">
// Here you define the click.
$("#fb_login").click(function(){
    $("#fb_login").colorbox({width:"300px", height:"200px", inline:true, href:"#loginask_iframe"});
});

// Here you call the click.
$("#fb_login").click();
</script>


回答2:

Is this what you're looking for?

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


回答3:

This may be a case of the click function being defined before the anchor is created. Try wrapping the click binding in $(document).ready( ...



回答4:

<script> 
function triggerLoginColorBox() {
    $("#fb_login").colorbox({
        width: "300px",
        height: "200px",
        inline: true,
        href: "#loginask_iframe"
    });
}
$("#fb_login").click(function(){triggerLoginColorBox();});
<?php
if (isset($_SESSION['cnt']) && ($_SESSION['cnt'] == 5) ){
    $_SESSION['cnt'] = 0;
    echo "triggerLoginColorBox();";
}
?>
</script>