简体   繁体   中英

Click button on page load with div content changed

I have links which change content on click. I want one of the links to be cliked on page load and stay as active. Looks like it works in the below fiddle but when I add it on website this one is loaded :

http://jsfiddle.net/0e91svrr/

I think I have some mistake in JS:

   <script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js"></script> 
<script type="text/javascript">//<![CDATA[
    $(document).ready(function() {
        $('a[class^=question]').click(function(e){
      e.preventDefault();
        var id = $(this).attr("class").replace("question","")
            $('.new_member_box_display').html($('#answer' + id).html());
        })
      document.getElementById("modal").click();

    });//end of ready
    </script>

The code you have written is perfectly fine. I would just suggest try JQuery's trigger click.

$('#modal').trigger('click');

First of all don't used Pure JS with JQuery mixed up, it look so messy :D also I sugest you think about your tags class and ids :)

Try first add event to links and then call click in document ready like this.

$('a[class^=question]').click(function(e){
    e.preventDefault();
    var id = $(this).attr("class").replace("question","")
    $('.new_member_box_display').html($('#answer' + id).html());
})

$(document).ready(function() {
  $("#modal").click();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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