<tfoot id='12ye4'></tfoot>

<small id='12ye4'></small><noframes id='12ye4'>

      <i id='12ye4'><tr id='12ye4'><dt id='12ye4'><q id='12ye4'><span id='12ye4'><b id='12ye4'><form id='12ye4'><ins id='12ye4'></ins><ul id='12ye4'></ul><sub id='12ye4'></sub></form><legend id='12ye4'></legend><bdo id='12ye4'><pre id='12ye4'><center id='12ye4'></center></pre></bdo></b><th id='12ye4'></th></span></q></dt></tr></i><div id='12ye4'><tfoot id='12ye4'></tfoot><dl id='12ye4'><fieldset id='12ye4'></fieldset></dl></div>

    1. <legend id='12ye4'><style id='12ye4'><dir id='12ye4'><q id='12ye4'></q></dir></style></legend>
      • <bdo id='12ye4'></bdo><ul id='12ye4'></ul>

      1. 如何在通过ajax加载的html中运行javascript

        how to run javascript in html loaded via ajax(如何在通过ajax加载的html中运行javascript)
      2. <tfoot id='qLeVV'></tfoot>

          <tbody id='qLeVV'></tbody>
      3. <legend id='qLeVV'><style id='qLeVV'><dir id='qLeVV'><q id='qLeVV'></q></dir></style></legend>

              <bdo id='qLeVV'></bdo><ul id='qLeVV'></ul>

                <i id='qLeVV'><tr id='qLeVV'><dt id='qLeVV'><q id='qLeVV'><span id='qLeVV'><b id='qLeVV'><form id='qLeVV'><ins id='qLeVV'></ins><ul id='qLeVV'></ul><sub id='qLeVV'></sub></form><legend id='qLeVV'></legend><bdo id='qLeVV'><pre id='qLeVV'><center id='qLeVV'></center></pre></bdo></b><th id='qLeVV'></th></span></q></dt></tr></i><div id='qLeVV'><tfoot id='qLeVV'></tfoot><dl id='qLeVV'><fieldset id='qLeVV'></fieldset></dl></div>

                  <small id='qLeVV'></small><noframes id='qLeVV'>

                  本文介绍了如何在通过ajax加载的html中运行javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 index.php 文件,它通过这个 javascript/ajax 代码加载其他 php 文件:

                  I have an index.php file that loads other php files via this javascript/ajax code:

                  function AJAX(elementID,url,showStatus){
                  var httpObject;
                  if (window.ActiveXObject) {
                      httpObject = new ActiveXObject("Microsoft.XMLHTTP");
                  }
                  if (window.XMLHttpRequest){
                      httpObject =  new XMLHttpRequest();
                  }
                  else {
                      alert("Your browser does not support AJAX.");       
                  }
                  
                  if (httpObject != null) {
                      httpObject.onreadystatechange = function() {          
                          if (elementID != false){                
                  
                              if (httpObject.readyState == 4 && httpObject.status == 200) {                  
                                  document.getElementById(elementID).innerHTML= httpObject.responseText;  
                  
                              } 
                          }
                  
                      }
                  
                      httpObject.open("POST",url,true);
                      httpObject.send(null);  
                  }
                  }
                  

                  例如,我将通过以下方式在 inxex.php 中加载一个文件:

                  so for example I would load a file in inxex.php by:

                  <script>
                  AJAX("updateThisDiv", "/includes/contentpage.php", false)
                  </script>
                  

                  这会将contentpage.php"的内容粘贴到 divupdateThisDiv"但现在如果我在contentpage.php"上有任何 javascript,它将无法运行,有什么办法吗?

                  which would paste the contents of "contentpage.php" into the div "updateThisDiv" but now if I have any javascript on "contentpage.php", it will not run, is there any way to do this?

                  我看过这个:http://www.javascriptkit.com/script/script2/ajaxpagefetcher.shtml但它并不是我想要的.

                  I have looked at this: http://www.javascriptkit.com/script/script2/ajaxpagefetcher.shtml but its not exatcly what I was looking for.

                  我希望能够在不重新加载整个页面的情况下更新页面的一部分并且必须运行 javascript

                  I want to be able to update a section of my page without reloading the entire page and javascript must run

                  推荐答案

                  如果你想按需加载 Javascript.这可以通过动态创建脚本标签来完成.这种模式在 Stoyan Stefanov 书 - Javascript 模式

                  If you want to load Javascript on demand. This can be done by dynamically creating script tag. This pattern illustrated in Stoyan Stefanov book - Javascript Patterns

                  这是从书中摘录的:

                  编写一个需求函数.然后这样称呼它:

                  Write a require function. Then call it like this:

                  require("extra.js", function () {
                      functionDefinedInExtraJS();
                  });
                  

                  示例需要函数:

                  function require(file, callback) {
                  
                      var script = document.getElementsByTagName('script')[0],
                          newjs = document.createElement('script');
                  
                      // IE
                      newjs.onreadystatechange = function () {
                          if (newjs.readyState === 'loaded' || newjs.readyState === 'complete') {
                              callback();
                          }
                      };
                  
                      // others
                      newjs.onload = function () {
                          callback();
                      };
                  
                      newjs.src = file;
                      script.parentNode.insertBefore(newjs, script);
                  }
                  

                  在 http://www.jspatterns.com/中找到的实时示例book/8/ondemand.html

                  @Edit:针对您的案例的更多详细信息.我会尽量让事情变得简单:

                  @ more details specific to your case. I will try to make things simple:

                  创建四个文件:

                  • index.php : 测试文件.
                  • code.js : 具有 Ajax、getJS、getHTML 函数的实际代码
                  • content.php : 任何无需 JS 即可打印纯 HTML 的 PHP 文件
                  • content.js :您想要动态运行的 javascript 代码.

                  index.php

                  <html>
                      <head>
                          <script src="code.js"></script>
                      </head>
                      <body>
                          <input type="button" onclick="Ajax('content.php', 'd_html');" value="Fill from content.php"/>
                          <div id="d_html"></div>
                          <br>
                          <input type="button" onclick="Ajax('content.js', 'd_js');" value="Fill from content.js"/>
                          <div id="d_js"></div>
                      </body>
                  </html>
                  

                  内容.php

                  <span>Hello, I am dynamic span came from content.php</span>
                  

                  content.js

                  //Ana javascript code you want to run it by Ajax function should go inside this function
                  function executeJS(element){
                     element.innerHTML = "<span>Hello, I am dynamic span came from content.js</span>";
                  }
                  

                  code.js

                  //This function responsible for doing the ajax request for any file that will return pure HTML.
                  function getHTML(url, element){
                      var i, xhr, activeXids = [
                          'MSXML2.XMLHTTP.3.0',
                          'MSXML2.XMLHTTP',
                          'Microsoft.XMLHTTP'
                      ];
                  
                      if (typeof XMLHttpRequest === "function") { // native XHR
                          xhr =  new XMLHttpRequest();        
                      } else { // IE before 7
                          for (i = 0; i < activeXids.length; i += 1) {
                              try {
                                  xhr = new ActiveXObject(activeXids[i]);
                                  break;
                              } catch (e) {}
                          }
                      }
                  
                      xhr.onreadystatechange = function () {
                          if (xhr.readyState !== 4) {
                              return false;
                          }
                          if (xhr.status !== 200) {
                              alert("Error, status code: " + xhr.status);
                              return false;
                          }
                  
                          element.innerHTML += xhr.responseText;
                      };
                  
                      xhr.open("GET", url, true); 
                      xhr.send("");
                  }
                  
                  //This function will load javascript file on-demand and call executeJS function inside that file.
                  function getJS(url, element, cb){
                      var newjs = document.createElement('script');
                  
                      // IE
                      newjs.onreadystatechange = function () {
                          if (newjs.readyState === 'loaded' || newjs.readyState === 'complete') {
                              cb();
                          }
                      };
                  
                      // others
                      newjs.onload = function () {
                          cb();
                      };
                  
                      newjs.src = url;
                      element.appendChild(newjs);
                  }
                  
                  
                  //This is same as your function, but now can handle both PHP and JS files
                  function Ajax(url, id){
                      var element = document.getElementById(id),
                          regex = /.js$/;
                      if(!element){
                          alert("Invalid ID");
                          return false;
                      }
                  
                      if(regex.test(url)){ //If url ends with JS, load using getJS
                          getJS(url, element, function(){
                              executeJS(element);
                          });
                      } else {
                          getHTML(url, element);
                      }
                  }
                  

                  这篇关于如何在通过ajax加载的html中运行javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                    <bdo id='9bRvH'></bdo><ul id='9bRvH'></ul>

                        • <i id='9bRvH'><tr id='9bRvH'><dt id='9bRvH'><q id='9bRvH'><span id='9bRvH'><b id='9bRvH'><form id='9bRvH'><ins id='9bRvH'></ins><ul id='9bRvH'></ul><sub id='9bRvH'></sub></form><legend id='9bRvH'></legend><bdo id='9bRvH'><pre id='9bRvH'><center id='9bRvH'></center></pre></bdo></b><th id='9bRvH'></th></span></q></dt></tr></i><div id='9bRvH'><tfoot id='9bRvH'></tfoot><dl id='9bRvH'><fieldset id='9bRvH'></fieldset></dl></div>
                        • <tfoot id='9bRvH'></tfoot>

                          <small id='9bRvH'></small><noframes id='9bRvH'>

                              <tbody id='9bRvH'></tbody>
                            <legend id='9bRvH'><style id='9bRvH'><dir id='9bRvH'><q id='9bRvH'></q></dir></style></legend>