我是 Laravel 的新手,我必须在我的代码中添加一个 Jquery 我该如何添加它?
$(document).ready(function(){
$('#selectAll').on('click',function(){
if ($(this).is(':checked')) {
$('.chkbox').each(function(){
this.checked = true;
});
}else{
$('.chkbox').each(function(){
this.checked = false;
});
}
})
});
If I am creating a file named
myjquery.js
- 我在哪里添加这个文件?
- 我应该如何链接它?或任何其他方法添加上述代码?
最佳答案
你必须把它附加到<script></script>
里面标签和包含 jquery 库。
如果您将它们(您的脚本和 jquery 库)都包含在页面末尾,就在结束之前 </body>
标签,甚至更好,因为可用性原因。
我的建议:
将此添加到主模板的底部:
.
.
.
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
jQuery(document).ready(function() {
@yield('postJquery');
});
</script>
</body></html>
这样您就可以确保在最后加载 jQuery 库和后续的 javascript 脚本。
然后,将其添加到需要脚本并扩展主模板的 View 中:
@section('postJquery')
@parent
$('#selectAll').on('click',function(){
if ($(this).is(':checked')) {
$('.chkbox').each(function(){
this.checked = true;
});
}else{
$('.chkbox').each(function(){
this.checked = false;
});
}
});
@endsection
这样你的脚本只包含在真正需要它的 View 中。您可以为您需要的任何脚本执行此操作。 @parent
需要部分以防止覆盖 postJquery
部分 View 已使用相同部分的 View 转换的部分 View 。
关于php - 如何在 Laravel 5 中使用我自己的 Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33757236/