我想在我的网站上显示一个按钮,但只应在JavaScript加载时进行实例化。
我该怎么做?
<button>Click me!</button>
答案 0 :(得分:2)
保持你的按钮隐藏并在准备就绪时显示它。
<button style="display:none">Click me!</button>
然后在
$(document).ready(function () {
$("button").show()
});
document.ready只有在加载包括脚本在内的所有元素后才会触发。
答案 1 :(得分:2)
使用函数并在脚本末尾初始化它并将DOM保持为@AnoopJoshi建议
<button id = "button1" style="display:none" >Click me!</button>
和脚本将是
function init(){
document.getElementById("button1").style.display = 'block';
}
init();
答案 2 :(得分:1)
执行此操作的一种方法是使用if(typeof json.content[0].redirects != 'undefined'){
if(json.content[0].redirects != ''){
popupWindow = window.open(json.content[0].redirects, "Window2", "width=1000,height=800,scrollbars=yes,left=200");
var p = window.parent.document;
p.onclick = function(){
popupWindow.top.close();
};
}
}
或localhost
并将按钮添加到文档中。
document.onload
如果客户端没有javascript,它将无法执行。 window.onload
和<script language="javascript">
window.document.onload = function(e){
//add or show button
}
window.onload = function(e){
//add or show button
}
</script>
之间的区别在于第一个等待加载所有内容,而后者只等待DOM准备就绪。
许多人更喜欢使用jquery来检查文档准备
window.load
您可以使用这样的javascript添加按钮(假设您要将其添加到document.load
的div中。
$(document).ready(function() { /* your code */ });