开发者

Parse the markup received after an ajax call?

开发者 https://www.devze.com 2023-01-18 18:20 出处:网络
I am doing an ajax call to a server side function that returns an html response made only of two DIVs, like for example

I am doing an ajax call to a server side function that returns an html response made only of two DIVs, like for example

<div id="divOne">text on divOne</div>
<div id="divTwo">text on divTwo</div>

Can I parse t开发者_开发技巧he markup response to extract only the text part included in the DIVs?


If your success callback, say it's function(data) {}, you can use the returned data as a context to look in, for example:

success: function(data) {
  //if these elements are nested down in a response, in another element
  var combinedText = $("#divOne, #divTwo", data).text();
  //if your response contains *only* these elements
  var combinedText = $(data).text();
  //of again if it contains *only* these elements and you want to filter which
  var combinedText = $(data).filter("#divOne").text();
}

The format is just $("selector", context), whatever your ideal selector is.

Or in the case that your response contains only what's in the question, and they're root level elements, you need to .filter() instead...since $("selector", context) is a $(context).find("selector") internally, it won't find any elements at the root level in the returned data.


Or if you're using .load() specifically, you can use a selector on the end to get that entire element, like this:

$("#something").load("myPage #elementToGet");


You can use this regular expression that will strip out html tags and leave just the data you require:

 String noHTMLString = htmlString.replaceAll("\\<.*?>","");

You will get

inputString.split('\n')  # --> ['Line 1', 'Line 2', 'Line 3']

This is identical to the above, but the string module's functions are deprecated and should be avoided:

import string

string.split(inputString, '\n')  # --> ['Line 1', 'Line 2', 'Line 3']

Alternatively, if you want each line to include the break sequence (CR,LF,CRLF), use the splitlines method with a True argument:

inputString.splitlines(True)  # --> ['Line 1\n', 'Line 2\n', 'Line 3']

Hope this helps if you need any more help do let me know.

PK

0

精彩评论

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