[英]Execute jquery script after page loaded
我尝试在页面加载后执行以下脚本。
首先,我包含了 jquery。
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
然后我写了我的脚本:
<script>
function scale()
{
$hc = document.getElementById("hc").innerHTML;
$he = document.getElementById("he").innerHTML;
$we = document.getElementById("we").innerHTML;
jQuery('#mydiv_cam') .css('height', '8000px');
jQuery('#camera') .css('height', $hc+"px");
jQuery('#viewport') .css('height', $he+"px");
jQuery('#viewport') .css('width', $we+"px");
}
</script>
这是我的身体标签
<body onload="scale();">
在身体内部我加载变量:
<p hidden="true" id="hc"> <?php echo $höhe_camera ?> </p>
<p hidden="true" id="he"> <?php echo $heightexplode[1] ?> </p>
<p hidden="true" id="we"> <?php echo $widthexplode[1] ?> </p>
但什么也没有发生。
如果您想在文档加载时执行您的功能,那么只需这样做:
$(document).ready(function(){
$hc = document.getElementById("hc").innerHTML;
$he = document.getElementById("he").innerHTML;
$we = document.getElementById("we").innerHTML;
jQuery('#mydiv_cam') .css('height', '8000px');
jQuery('#camera') .css('height', $hc+"px");
jQuery('#viewport') .css('height', $he+"px");
jQuery('#viewport') .css('width', $we+"px");
//put everything else inside here
});
这会将此函数设置为文档的就绪函数,以便在文档完全加载后立即执行。 您也可以删除 body 标签上的属性。 此外,您可能想要修复 jQuery 导入:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
只需删除 php 标签前后的空格。 写成如下:
<p hidden="true" id="hc"><?php echo $höhe_camera ?></p>
<p hidden="true" id="he"><?php echo $heightexplode[1] ?></p>
<p hidden="true" id="we"><?php echo $widthexplode[1] ?></p>
这将起作用。 当您在 jquery 中使用 p 标签的 innerhtml 时,它也接受空格,并且在您将它与 px 连接后,它将被视为1 px
是样式表,它不起作用。 尝试一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.