页面加载时加载Javascript

页面加载时加载Javascript,javascript,jquery,html,function,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 printin

如何在页面加载时加载此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."
        });
    });

});
假设swal是打开模态的,只需将其移到click事件之外即可。当前代码仅在单击带有.demo1类的元素后运行swal函数。通过将其移到click handler函数之外,它会在页面准备就绪后立即运行

$(function() {
    swal({
        title: 'welcome',
        text: 'ding!'
    });
});
假设swal是打开模态的,只需将其移到click事件之外即可。当前代码仅在单击带有.demo1类的元素后运行swal函数。通过将其移到click handler函数之外,它会在页面准备就绪后立即运行

$(function() {
    swal({
        title: 'welcome',
        text: 'ding!'
    });
});
$function{ $“按钮”。单击功能{ 哇!你点击了我。 } }; 点击我 $function{ $“按钮”。单击功能{ 哇!你点击了我。 } };
单击我因为您使用的是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>

因为您使用的是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>
只需添加上述脚本,这将在页面加载时触发您的sweet警报


只需添加上面的脚本,这将在页面加载时触发您的sweet警报。

您可以使用bootstrap吗?您的问题缺少很多信息。什么是swal?你在使用模态插件吗?您是否正在尝试使用自己的模式?@Soviut我使用sweet Alerts,所以请在页面加载时调用它,而不是单击…您可以使用bootstrap吗?您的问题缺少很多信息。什么是swal?你在使用模态插件吗?你是在尝试使用你自己的模式吗?@Soviut我使用sweet alertso在页面加载时调用它,而不是单击。。。。