[英]Execute 2 scripts in 1 page
Why does only second javascript execute and not the first one?为什么只执行第二个 javascript 而不是第一个? Maybe PHP also executs the second code only?也许 PHP 也只执行第二个代码?
How can i execute both codes?我怎样才能执行这两个代码?
<div id="anslut0"></div> <div id="anslut1"></div> <?php $homepage = file_get_contents('http://X.com/2.php'); echo $homepage; ?> <script> window.onload = function(){ if (document.getElementById("MixP0").innerHTML <= 5) { document.getElementById("MixP0").style.visibility = "hidden"; document.getElementById("anslut0").innerHTML = "anslut0"; } } </script> <?php $homepage = file_get_contents('http://X.com/3.php'); echo $homepage; ?> <script> window.onload = function(){ if (document.getElementById("MixP1").innerHTML <= 5) { document.getElementById("MixP1").style.visibility = "hidden"; document.getElementById("anslut1").innerHTML = "anslut1"; } } </script>
You set window.onload
two times, so the second one overrides the first !您设置window.onload
两次,所以第二个覆盖第一个! onload is called AFTER page has finished loading, not during the parsing of html. onload 在页面加载完成后调用,而不是在解析 html 期间调用。
Get familiar with addEventListener : https://developer.mozilla.org/fr/docs/Web/API/EventTarget/addEventListener熟悉 addEventListener : https : //developer.mozilla.org/fr/docs/Web/API/EventTarget/addEventListener
Keep it Simple :) .保持简单:) 。 Create Two functions and call them from single window.onload
event.创建两个函数并从单个window.onload
事件调用它们。
<div id="anslut0"></div>
<div id="anslut1"></div>
<?php
$homepage = file_get_contents('http://X.com/2.php');
echo $homepage;
?>
<script>
function first() {
if (document.getElementById("MixP0").innerHTML <= 5) {
document.getElementById("MixP0").style.visibility = "hidden";
document.getElementById("anslut0").innerHTML = "anslut0";
}
}
</script>
<?php
$homepage = file_get_contents('http://X.com/3.php');
echo $homepage;
?>
<script>
function second() {
if (document.getElementById("MixP1").innerHTML <= 5) {
document.getElementById("MixP1").style.visibility = "hidden";
document.getElementById("anslut1").innerHTML = "anslut1";
}
}
window.onload = function() {
first();
second();
}
</script>
To execute both, but both in a single <script>
at the end.执行两者,但最后都在一个<script>
中。
<script>
window.onload = function(){
if (document.getElementById("MixP0").innerHTML <= 5) {
document.getElementById("MixP0").style.visibility = "hidden";
document.getElementById("anslut0").innerHTML = "anslut0";
}
if (document.getElementById("MixP1").innerHTML <= 5) {
document.getElementById("MixP1").style.visibility = "hidden";
document.getElementById("anslut1").innerHTML = "anslut1";
}
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.