javascript - 在页面加载时加载 Javascript

标签 javascript jquery html function

如何在页面加载时加载此 javascript 模式?它来自插件“甜蜜警报”。我对 javascript 真的很不好。任何建议一定会有所帮助!

<script>
$(function () {

    $('.demo1').click(function(){
        swal({
            title: "Welcome in Alerts",
            text: "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
        });
    });

});

最佳答案

由于您使用的是 jquery,因此您可以使用 document.ready 来包装代码

<script>
$(document).ready(function() {
  $('.demo1').click(function() {
    swal({
      title: "Welcome in Alerts",
      text: "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
    });
  });
})
</script>

现在,如果您将此片段放在结束 body 附近,则可以避免 document.ready

<body>
   //rest of the dom element
   <script>
    $('.demo1').click(function() {
        swal({
          title: "Welcome in Alerts",
          text: "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
        });
      });
    </script>
</body>

关于javascript - 在页面加载时加载 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41971922/

相关文章:

javascript - 通过动态更改内容让 IE 尊重您的空白

javascript - Node : chaining exec commands with promises

html - 带有文本的 Div 移动其他元素

javascript/html5 - 文本到图像

javascript - Element.focus() + IE 10+ 中的 css 动画定位问题

javascript - 监听器之前的 jQuery 触发器

javascript - Woocommerce 迷你购物车小部件产品价格覆盖

jQuery onclick 必须单击两次才能出现模式对话框

javascript - jquery 在 <ul> 中显示项目列表

javascript - 在 Javascript 列表中随机排列单词