简体   繁体   English

单击页面加载上的按钮,更改了div内容

[英]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/ http://jsfiddle.net/0e91svrr/

I think I have some mistake in JS: 我认为我在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. 我只是建议尝试使用JQuery的触发点击。

$('#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 :) 首先,不要将Pure JS与JQuery混合使用,它看起来太凌乱了:D我也很高兴您想到了您的标签类和id :)

Try first add event to links and then call click in document ready like this. 尝试首先将事件添加到链接,然后像这样准备好在文档中单击click。

$('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();
});

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

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