douaikuai2715 2018-10-26 13:07
浏览 243
已采纳

如何将onload()事件添加到laravel的@section('content')中

Is possible to do this? For example I have this code:

@section('content' onload='MyFunction')
  <!--PAGE CONTENT...-->
  <div>
   <h1>Something...</h1>
   <select name="sel"></select>
  </div>
@endsection

<script type="text/javascript">

  function MyFunction(){

   obj = document.forms[0].sel;

   for (i=0; i<23; i++) {

    //A similar condition to onload() event.

  }
</script>

Or is it wrong of me to do that so? I'm working in Laravel and PHP. Or maybe using a Jquery function similar to the function in javascript.

  • 写回答

3条回答 默认 最新

  • douxiong1994 2018-10-26 13:37
    关注

    You must understand that @Section is a blade tag. Blade tags are processed inside PHP. Also, the blade syntax does not accept the parameter onload as you want.

    Also, either on HTML/JS you can't use onload as you want.

    You have two alternatives:

    1 - Put a script tag after the section:

    @section('content')
      <!--PAGE CONTENT...-->
      <div>
       <h1>Something...</h1>
       <select name="sel"></select>
      </div>
      <script>
        // Note that this function must have been declared before
        // this script tag, otherwise it will log an error:
        // "Uncaught ReferenceError: MyFunction is not defined"
        MyFunction(); 
      </script> 
    @endsection
    

    In this solution, the function will be called right after the browser loads this piece of html, but other parts of the html may not be loaded yet.

    2 - Put a script tag watching for the load event on document:

    <script>
        $(document).load(function() {
            MyFunction(); 
        });
    </script>
    

    The advantage of this method is that it will only be called after the entire page being loaded, so the order of the <script> tags doesn't matters (except that the jquery call must be after the jquery script tag.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名