开发者

jQuery html() function with javascript

It appears that the .html function doesn't return javascript. Is there a function that returns both javascript and html?

EDIT:

Example:

Main Page

$.ajax({        
  url: 'ajaxpage.php',
  type: 'post',
  data: postvars,
  cache: false,
  dataType: 'html',
  success: function (html) {
    $('#content').html($(html).find('#content').html());
  }
});

ajaxpage.php

<html>
<body>
<div id="content">
   //SOME HTML
   <script type="text/javascript">
开发者_StackOverflow      //SOME JAVASCRIPT
   </script>
</div>
</body>
</html>

$(html).find('#content').html() returns

   //SOME HTML

while I want

   //SOME HTML
   <script type="text/javascript">
      //SOME JAVASCRIPT
   </script>


Unfortunately it doesn't appear that there is something that will allow the javascript to stay using jquery. Most functions strip out the javascript and others show the whole page, which just using the html variable from the success function would do.

The returned html variable has the javascript and you can just strip out what you don't need from there.

var htmlcontent = html.match(/<div\s+id="content">[\S\s]*?<\/div>/);
if (htmlcontent != null) {
   $('#content').html(htmlcontent[0]);
}


Try using .load() instead of .html()


Looks like some of jQuery's insertion methods (like html or append) process the code for possible injections, removing script tags along the way. You can take a look at the answers to this question, or see the relevant comment in the jQuery API docs.


I have tried with filter but it only return the html part

 $('#content').html($(html).filter('#content'));

But when I tried with filter along with prevObject

i.e

$('#content').html($(html).filter('#content').prevObject);

it returns the html part (visible in firebug) as well as the javascript(not visible in firebug).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜