简体   繁体   English

在 1 页中执行 2 个脚本

[英]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.

相关问题 mvc 4底部的脚本 - 不执行 <scripts> 在视图页面中 - scripts at the bottom in mvc 4 - Does not execute <scripts> in View page 如何避免在Javascript消息框中选中“在此页面上停止执行脚本”复选框? - How to avoid the checkbox 'Stop execute scripts on this page' in the Javascript message box? 如何在HtmlUnit中执行JavaScript而不运行页面脚本 - How to execute JavaScript in HtmlUnit without running page scripts 如何从本地计算机上的网页执行Python和.bat脚本 - How to execute Python and .bat scripts from a web page on local machine Selenium:如何在加载/执行页面的任何其他脚本之前将 Javascript 注入/执行到页面中? - Selenium: How to Inject/execute a Javascript in to a Page before loading/executing any other scripts of the page? 脚本的加载和执行顺序 - load and execute order of scripts 执行AJAX返回的脚本 - Execute scripts AJAX returns 如何通过javascript / jquery从网页执行shell脚本并以字符串形式获取其结果? - How to execute shell scripts from a web page via javascript/jquery and get its results in a string? 异步脚本在延迟后执行 - Asynchronous scripts execute after deferred 在其他脚本之后执行jQuery? - Execute jQuery after other scripts?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM