dongzhan0624 2017-08-14 11:16
浏览 53
已采纳

在functions.php中执行document.ready

I need to run a script after a hook is fired in my functions.php file. Im using a wordpress mailchimp plugin, I need to do some text inserts and display = "none", etc, in the html:

add_action( 'mc4wp_form_success', function() {
   // do something
     echo '<script type="text/javascript">',
        'console.log("starting....");', // this works
     'document.getElementById("mailchimp-is-subscribed").style.display = "none";',
         'document.getElementById("mailchimp-success-form").style.display = "block";',
         'console.log("end");',
     '</script>';
});

The error I got was style is null. I assume the document is not ready? $ does not work. Am I doing this wrong? I need to run that code after that hook is called. Putting the code in .js file works great. Thanks

  • 写回答

1条回答 默认 最新

  • dtcuv8044 2017-08-14 11:23
    关注

    You can add your code as an on DOMContentLoaded event:

    document.addEventListener('DOMContentLoaded', function () { ..your_code... }, false);

    Check this answer: pure JavaScript equivalent to jQuery's $.ready() how to call a function when the page/dom is ready for it

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改