Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从响应中获取html和javascript_Javascript_Jquery - Fatal编程技术网

如何从响应中获取html和javascript

如何从响应中获取html和javascript,javascript,jquery,Javascript,Jquery,我有以下javascript $.ajax({ type: ... url: ... data: ... bla bla... success: function( html ) { var response = $( html ).filter( '#targetID' ).html(); $( 'body' ).html( response ).show('slow'); } }); 除了用javascript加载页面外,它工

我有以下javascript

$.ajax({
    type: ... url: ... data: ... bla bla...

    success: function( html ) {

        var response = $( html ).filter( '#targetID' ).html();

        $( 'body' ).html( response ).show('slow');

    }
});
除了用javascript加载页面外,它工作得很好,javascript会消失。
那么如何解决这个问题呢?

您的意思是通过Ajax请求获得javascript

在这种情况下,你需要确保你有正确的代码在里面。因为compability是那种东西的婊子。 JQuery duos是一个eval函数,我认为当他加载结果时,js应该加载。但可能是您的代码有一些在页面上通常被忽略的aprse错误,但当您将其与eval一起使用时,在某些浏览器中会出现错误

你用的是哪个兄弟


将代码加载到指定的分区后,尝试对其求值。

您应该在正文中使用容器,而不是将代码直接放入正文中

即,在主体内添加div标签,该主体将成为容器

<div class="container"></div>
这样,内容加载到容器中,而不是直接加载到正文中,正文将替换页面的所有内容,包括您在那里的JS

另外,在使用Ajax调用时,我相信它可以使代码更清晰地将响应传递给其他函数进行处理。这样,您就可以调试更小的函数,并且更容易理解代码

$.ajax({
  url: 'ajax/test.html',
  success: function(data) {

      //Here you pass the response to the other function
      processSuccess(data);
  },
  fail: function(data) {

      //Here you pass the response to the other function
      processFail(data);
  }

});

function processSuccess(response) {
     //Print the response in the console (ie. Firebug console) 
     //if console is available
     if(console) {
        console.log(response);
     }
};

function processFail(response) {
     //Print the response in the console (ie. Firebug console) 
     //if console is available
     if(console) {
        console.log(response);
     }
};

当然,命名空间内的所有内容都会使它更好。

如果脚本在体标记中,HTML-()方法将清除所有内容,考虑将数据附加到另一个元素而不是体元素。<代码>我有以下JavaScript < /C> >代码> $Ajax({type:…url:…data:…bla bla…。这是如何执行的?你说的“javascript消失”是什么意思?它是否与页面一起加载?这里的问题类似:没关系,但我只需要响应中的一个特定div。有时这个div可以包含javascript
$.ajax({
  url: 'ajax/test.html',
  success: function(data) {

      //Here you pass the response to the other function
      processSuccess(data);
  },
  fail: function(data) {

      //Here you pass the response to the other function
      processFail(data);
  }

});

function processSuccess(response) {
     //Print the response in the console (ie. Firebug console) 
     //if console is available
     if(console) {
        console.log(response);
     }
};

function processFail(response) {
     //Print the response in the console (ie. Firebug console) 
     //if console is available
     if(console) {
        console.log(response);
     }
};