简体   繁体   English

JavaScript:防止在加载整个页面之前按下按钮

[英]JavaScript: Prevent a button from being pressed before entire page is loaded

How can I prevent users from pressing my web-page buttons before the page is fully loaded? 如何防止用户在页面完全加载之前按下我的网页按钮?

ie I have a button that opens a lightbox. 即我有一个打开灯箱的按钮。 The JS for the lightbox is loaded last in the page for quicker response time. 灯箱JS会在页面的最后加载,以缩短响应时间。 So if the user presses the button before the page is fully loaded - he either gets a JS error or the lightbox data is opened on a blank page. 因此,如果用户在页面完全加载之前按下按钮-他会收到JS错误或在空白页面上打开灯箱数据。

I'm using Javascript with MooTools. 我在MooTools中使用Javascript。

If I understand correctly neither 如果我没有正确理解

<body onload="...">

nor 也不

$('#yourButton')...

guarantees that the document has finished loading. 保证文档已完成加载。 The (jQuery) method that does: (jQuery)方法执行以下操作:

$(document).ready(function() { 
    $('#yourButton').attr("disabled", false); 
});

Make your button disabled. 禁用按钮。 After the page is loaded - make it enabled 页面加载后-启用它

Here is a JQuery(sorry for not being "clean" Javascript example) 这是一个JQuery(对不起,“ JavaScript”示例不干净)

$('#yourButton').attr("disabled", false);

here's an example: 这是一个例子:

<body onload="document.getElementById('button1').disabled=false">
  <button id="button1" disabled="1">
    ok
  </button>
<body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM