Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
每个example.html文件(example.blade.php)或common.js中最好的调用DOM控件javascript函数是什么?_Javascript_Php_Html_Laravel - Fatal编程技术网

每个example.html文件(example.blade.php)或common.js中最好的调用DOM控件javascript函数是什么?

每个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) //并非每个示

例如,在一个项目中,有许多示例*.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)
    
    //并非每个示例*.html(示例*.blade.php)中都包含此id=“test”
    common.movingTest();
    
  • 案例2。调用common.js
  • 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文件中硬编码