每个example.html文件(example.blade.php)或common.js中最好的调用DOM控件javascript函数是什么?
例如,在一个项目中,有许多示例*.html(示例*.blade.php)每个example.html文件(example.blade.php)或common.js中最好的调用DOM控件javascript函数是什么?,javascript,php,html,laravel,Javascript,Php,Html,Laravel,例如,在一个项目中,有许多示例*.html(示例*.blade.php) common.js var公共={ movingTest:function(){ document.getElementById(“测试”).addEventListener('click', 函数(){ //移动测试 } ); } } 案例1。调用每个文件 (例如,示例1、示例3、示例5具有id=“test”,但不具有示例2、示例4、示例6) html(example1.blade.php) //并非每个示
- common.js
var公共={
movingTest:function(){
document.getElementById(“测试”).addEventListener('click',
函数(){
//移动测试
}
);
}
}
- html(example1.blade.php)
//并非每个示例*.html(示例*.blade.php)中都包含此id=“test”
common.movingTest();
var公共={
movingTest:function(){
document.getElementById(“测试”).addEventListener('click',
函数(){
//移动测试
}
);
},
}
//不是每个测试都在.html文件中,我写了“如果”
if(document.getElementById(“test”).length!==0){
common.movingTest();
}
你的做法是什么?
或者我可以通过任何其他工具发现什么是最好的
谢谢。案例1通常是包含敏感javascript文件的最佳实践,因为它们位于html文件之外 一般不建议使用案例2,由于安全问题,案例2不是最佳做法。如果js文件太大,也会影响页面加载速度 所以案例1是最好的,总是包含js文件,而不是在html文件中硬编码