ready

  <script>
	 $(document).ready(function(){
		$('#test1').on('click', function(){
			layer.open({
			  type: 1,
			  skin: 'layui-layer-rim', //加上边框
			  area: ['420px', '240px'], //宽高
			  closeBtn: 1,
			  content: $('#box')
			});
		});
	});
</script>
<body>
          <button id="test1">小小提示层</button>
         <div id="box">这是条测试数据</div>
</body>

  

这块script代码写在body之外时,需要用ready() 方法;如果写在body里面则不需要这个方法。

<body>
  <button id="test1">小小提示层</button>
  <div id="box">aaaaaaaaaa</div>
  <script>
		$('#test1').on('click', function(){
			layer.open({
			  type: 1,
			  skin: 'layui-layer-rim', //加上边框
			  area: ['420px', '240px'], //宽高
			  closeBtn: 1,
			  content: $('#box')
			});
		});
   </script>
</body>

  而且script放在button上面也是没有效果的

jQuery 事件 - ready() 方法

定义和用法

当 DOM(文档对象模型) 已经加载,并且页面(包括图像)已经完全呈现时,会发生 ready 事件。

由于该事件在文档就绪后发生,因此把所有其他的 jQuery 事件和函数置于该事件中是非常好的做法。正如上面的例子中那样。

ready() 函数规定当 ready 事件发生时执行的代码。

ready() 函数仅能用于当前文档,因此无需选择器。

原文地址:https://www.cnblogs.com/wangbei2016/p/7245130.html