首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >javascript未执行插入到内部HTML中

javascript未执行插入到内部HTML中
EN

Stack Overflow用户
提问于 2014-05-15 00:28:48
回答 1查看 82关注 0票数 0

我正在尝试从另一个页面获取响应,例如,它的名称是ajaxresponse.php,通过ajax请求,我还想在该ajaxresponse.php页面上执行一些由java脚本完成的操作,但js代码不起作用。我正在从该页面获得响应,但该页面的js代码不起作用。我只想知道为什么我的第二个页面上的ajax代码不工作?

代码语言:javascript
复制
function ajaxinput(tinnerid)
{
    var xmllhttp;
       if (window.XMLHttpRequest)
        {
          xmlhttp=new XMLHttpRequest();
        }
       else
        {
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
       xmlhttp.onreadystatechange=function()
        {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
           {
            document.getElementById("articledive").innerHTML=xmlhttp.responseText;
           }
        }
    xmlhttp.open("GET","ajaxresponse.php?q="+tinnerid,true);
    xmlhttp.send();
}

这是第一页的php代码

代码语言:javascript
复制
<?php
 $con1=mysqli_connect("127.0.0.1","root","root","databasetry");
 $result=mysqli_query($con1,"select title,articleid  from article");
 $tinnerid=0;
 $articledivid=0;
   while($row = mysqli_fetch_array($result))
    {
     $tinnerid=$row['articleid'];
     echo "<div class='tinner' id='$tinnerid' onclick='ajaxinput($tinnerid)'    style='cursor:pointer;'>";
     echo"<span class='fontdiv'>";
     echo $row['title']."<br>";
     echo "</span>";
     echo"</div>";
     $tinnerid++;
     $articledivid++;
    }
 mysqli_close($con1);
?>

下面是第二页的js代码。

代码语言:javascript
复制
function run()
{
 alert("example");
}

下面是第二页的php代码

代码语言:javascript
复制
 <?php
  $id=$_GET['q'];
  $result=mysqli_query($con1,"select user.username,article.articleid,article.title,article.artiletext from article inner join user on article.user_id=user.user_id where article.articleid=$id");
 $divid=0;
    while($row = mysqli_fetch_array($result))
     {   
      $id=0; 
      echo"<div class='inner' id=$divid >";
      $id=$row['articleid'];
      echo "<font class='ftitle'>"."Title : "."<a href='#'onclick='ajaxinput($id)'>".$row['title']."</a>"."</font>"."<br>"."<font class='ftext'>".$row['artiletext']."</font>"."<br>"."<font class='flast'>"."Posted By : ".$row['username']."</br>"."</font>"."<br>"."<br>";    
      echo "<button type='button' onclick='con($id)'  class='button'><font size='1'>Delete</font></button>";
      echo"</div>";
      $divid++;
      echo"<script>run()</script>";   
      }
  mysqli_close($con1);
 ?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-15 00:39:06

在运行时作为innerHTML插入的Javascript将不会被执行。一种可能的解决方案是从第二个页面返回中删除脚本标记,并在ajax回调之后调用第一个页面上的run()方法。如下所示:

代码语言:javascript
复制
function ajaxinput(tinnerid)
{
   var xmllhttp;
   if (window.XMLHttpRequest)
    {
      xmlhttp=new XMLHttpRequest();
    }
   else
    {
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
   xmlhttp.onreadystatechange=function()
    {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
       {
        document.getElementById("articledive").innerHTML=xmlhttp.responseText;

        /* CALL IT HERE AFTER INSERT */
        run();
       }
    }
xmlhttp.open("GET","ajaxresponse.php?q="+tinnerid,true);
xmlhttp.send();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23660256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档