最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting inner html with jQuery? - Stack Overflow

programmeradmin2浏览0评论

I have a javascript/jQuery block as a callback after $.get function:

function myCallBack(data, textStatus) {
  var text1 = $(data).html();
  document.write(text1);
} 

The data contains html data ok. I'd like to strip the html and get only inner html into text1 variable. For some reason it doesn't work. Firebug kinda "crashes" upon executing line 'var text1 = ...'

Edited:

My data variable contains:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "w3/TR/xhtml1/…;\r\n\r\n<html xmlns="w3/1999/xhtml">;\r\n
<head>\r\n 
<title></title>\r\n
</head>\r\n
<body>\r\n Testing...\r\n</body>\r\n
</html>\r\n 

And I'd like to parse the part between body tags.

I have a javascript/jQuery block as a callback after $.get function:

function myCallBack(data, textStatus) {
  var text1 = $(data).html();
  document.write(text1);
} 

The data contains html data ok. I'd like to strip the html and get only inner html into text1 variable. For some reason it doesn't work. Firebug kinda "crashes" upon executing line 'var text1 = ...'

Edited:

My data variable contains:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "w3/TR/xhtml1/…;\r\n\r\n<html xmlns="w3/1999/xhtml">;\r\n
<head>\r\n 
<title></title>\r\n
</head>\r\n
<body>\r\n Testing...\r\n</body>\r\n
</html>\r\n 

And I'd like to parse the part between body tags.

Share Improve this question edited Jul 6, 2009 at 9:55 Pompair asked Jul 6, 2009 at 9:08 PompairPompair 7,31912 gold badges64 silver badges69 bronze badges 2
  • Did you try innerHTML? – Gumbo Commented Jul 6, 2009 at 9:12
  • Just tried. var text1 = data.innerHTML; Returns "undefined". – Pompair Commented Jul 6, 2009 at 9:59
Add a ment  | 

3 Answers 3

Reset to default 6

You mean you want the inner text?

var text1 = $(data).text();

[Update]

Try it with this regular expression:

var bodyText = new RegExp(/<body[^>]*>([\S\s]*?)<\/body>/).exec(data)[1];

Try this:

$(data)[1].data

But I think that just works with a specific example and not in general.

You perform a webrequest by get. That implies that the result will only be a string.

var text1 = data ;

Is all you can get. There is no DOM-object you can traverse. You only get this if you access elements on you own page.

发布评论

评论列表(0)

  1. 暂无评论