我想在HTML页面中的焦点事件之前调用onload事件。在下面的代码中
<!DOCTYPE html>
<html>
<head>
<body onload='onloading()'>
<script src="jquery.min.js"></script>
<script>
$(document).ready(function(){
$(window).focus(function(){
callMe();
});
});
function callMe()
{
alert('Focus is on current page');
}
function onloading()
{
alert('Onload function call ');
}
function myFunction()
{
document.write("some text");
}
document.getElementById("demo").innerHTML = myFunction();
</script>
</head>
<p id="demo"></p>
</body>
</html>
当我执行这段代码时,我会看到一个弹出:“聚焦在当前页面上”,在后台是“一些文本”。但我希望按以下顺序得到输出
但我不会弹出"Onload函数调用“:我需要在焦点事件之前在体内运行onload事件。做这样的事有可能吗?
发布于 2016-01-25 15:29:17
试试这个:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
<p id="demo"></p>
<p id="focus">Click here</p>
<script type="text/javascript">
function callMe()
{
$("#focus").html('Focus is on current page');
}
function onloading()
{
alert('Onload function call ');
}
function myFunction()
{
$("#demo").html("some text");
}
$(window).load(function () {
onloading();
});
$(document).ready(function(){
$("#demo").html= myFunction();
$(window).focus(function(){
callMe();
});
});
</script>
</body>
</html>
发布于 2016-01-25 14:49:15
您的HTML无效。您已经将head
标记放置在body
中,这是不允许的。
利用以下结构:
<html>
<head>
<script></script>
</head>
<body>
</body>
</html>
https://stackoverflow.com/questions/34995232
复制相似问题