Regular expression and working with buffer instead of jquery and dom selectors
After developing some javascript classes and scripts, i noticed that there is a big speed problem with jquery selectors. My application actually is designed to work with components, written in javascript. The problem is - I want to have anything in string buffer before it gets to client's browser. Maybe it wouldn't be complicated, but i use some jquery selectors to manipula开发者_JAVA技巧te components inside html. Is there a solution to work with $('...') on a string? thx
Probably the best way to manipulate HTML on the client is to put it inside an element (say a div via its innerHTML property) and use DOM methods (getElementById, getElementsByTagName and getElementsByClassName are usually sufficient). The browser's HTML parser will parse it and turn it into a DOM fragment far faster than you can parse it, and you only have to do it once. As long as you don't append the fragment to the document, it won't be visible.
Since you seem to be using HTML as a data transport mechanism, have you considered XML? You can create an XML document and use XPath, which is very fast. Or you could use JSON, which really just means using a native object to hold the data rather than a document, but getting the data out requires manual traversal wich is likely slower than DOM or XPath methods.
Also, rather than building an HTML string and inserting it as the innerHTML of some element, you are likely better off to build your fragment for insertion using DOM methods. That way you don't have to worry about generating valid HTML, you just clone elements from your data document fragment and modify their properties as required, then insert it in the document.
精彩评论