Javascript以下代码行之间的区别是什么
当我们使用jquery时,以下代码行之间的区别是什么。他们中有谁更可取吗Javascript以下代码行之间的区别是什么,javascript,jquery,Javascript,Jquery,当我们使用jquery时,以下代码行之间的区别是什么。他们中有谁更可取吗 <script> alert("hello"); // 1. alert hello $(function(){alert("hello");}); // 2. Also alert hello (function($){alert("hello");})(jQuery); // 3. It also alert hello </script> 警报(“你好”);//1.警惕你好 $(函数(
<script>
alert("hello"); // 1. alert hello
$(function(){alert("hello");}); // 2. Also alert hello
(function($){alert("hello");})(jQuery); // 3. It also alert hello
</script>
警报(“你好”);//1.警惕你好
$(函数(){alert(“hello”);});//2.也提醒你好
(函数($){alert(“hello”);})(jQuery);//3.它也提醒你好
- 在第一行中,您只需调用
alert()
- 在第二行中,您试图获取匹配元素的集合,这些元素要么是基于传递的参数在DOM中找到的,要么是通过传递HTML字符串()创建的。因此,将计算参数并调用
alert()
- 在第三行中,调用一个自调用函数,并将jQuery作为参数传递,但不使用它。并调用
alert()
第一行
警报(“hello”)
绝对是最好的,因为最后两行只是为了提醒而导致了无用的计算。可能的重复只是编写一个自调用函数的一种方式,而不是在所有地方显示“hello”,alertthis
,如alert(this)
,您将了解更多信息。