CiberSkull Posted November 12, 2009 at 03:50 PM Report Share #295839 Posted November 12, 2009 at 03:50 PM Viva, eu pesquisei no forum e no google mas n encontrei nada... O que se passa é que quero fazer um parser. imaginem: <div id="content"> <p> blah</div> <p> blah1</div> <p> blah2</div> <p> blah3</div> <p> blah4</div> <p> blah5</div> </div> a função tem que retirar o conteudo da div e retornar um array de strings com: "blah1",blah2", etc. Para ir a div um getelementbyid chega. O meu problema e saber como pegar em cada <p> uma vez que estes não têm ID (e sobre isso eu não tenho controlo). O que sera: Document.getelementebyID.??? Obrigado a todos. Link to comment Share on other sites More sharing options...
yoda Posted November 12, 2009 at 07:47 PM Report Share #295893 Posted November 12, 2009 at 07:47 PM jQuery resolve, se quiseres uma solução nisso. before you post, what have you tried? - http://filipematias.info sense, purpose, direction Link to comment Share on other sites More sharing options...
CiberSkull Posted November 13, 2009 at 08:31 PM Author Report Share #296057 Posted November 13, 2009 at 08:31 PM Thanks 😄 fica aqui a minha resolução para quem vier pesquisar ao forum 😄 <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript"> $(document).ready(function(){ var htmlstr; htmlstr = $("#content").html().split("\n"); var line = new Array(); var i ; var k = htmlstr.length; var j = 1; for(i = 0 ; i<k;i++) { htmlstr[i] = htmlstr[i].slice(htmlstr[i].toString().indexOf(">")+1, htmlstr[i].toString().lastIndexOf("<")); if(htmlstr[i].toString() !=" " && htmlstr[i].toString().length >0) line[j++]=htmlstr[i].toString() ; } alert(line); }); Pode não ser a melhor solução mas funciona 😛 Thanks again Link to comment Share on other sites More sharing options...
yoda Posted November 13, 2009 at 11:27 PM Report Share #296104 Posted November 13, 2009 at 11:27 PM Não precisavas desse código todo 😄 Se quiseres uma solução mais "barata" apita. before you post, what have you tried? - http://filipematias.info sense, purpose, direction Link to comment Share on other sites More sharing options...
CiberSkull Posted November 14, 2009 at 04:39 AM Author Report Share #296135 Posted November 14, 2009 at 04:39 AM Sempre gostei de pagar pouco 🙂 Se não te importares, era da maneira que aprendia 😛 Thanks Link to comment Share on other sites More sharing options...
jreis Posted November 14, 2009 at 04:30 PM Report Share #296180 Posted November 14, 2009 at 04:30 PM Não duvido que o código que postaste funciona, mas e se por acaso a estrutura mudar? Se houver 2 <p> seguidos sem newline pelo meio, por exemplo? ou se um dos <p> tiver outro elemento dentro? Usando o DOM, consegues atravessar a estrutura toda sem nunca teres de saber nada sobre a dita. O Mozilla Developper Center tem uma documentação muito boa sobre isso, dá uma olhadela para teres uma ideia da coisa. Ou usa jQuery se tiveres com pressa/preguiça! 🙂 "Pensa positivo: tudo pode piorar" Link to comment Share on other sites More sharing options...
CiberSkull Posted November 14, 2009 at 05:59 PM Author Report Share #296188 Posted November 14, 2009 at 05:59 PM Até não me importava de fazer dessa maneira, mas tenho prazos a cumprir e eles são apertados.... Estou a curtir JQuery mas tenho muita pouca experiência no que toca a programação web e pior ainda: não tenho tempo... Mas jreis, obrigado 🙂 vou ler isso ate porque parece interessante, mas n vai puder ser em tempo útil para aquilo que preciso... thanks Link to comment Share on other sites More sharing options...
yoda Posted November 14, 2009 at 11:10 PM Report Share #296234 Posted November 14, 2009 at 11:10 PM Será algo parecido a isto. Deves também querer meter algum tipo de verificação quando sacas o conteudo dos parágrafos para ver se estão vazios ou se têm alguma coisa escrita. (Não testado) var arr = new Array(); $('#content p').each(function (i) { arr[i] = $(this).text(); }); before you post, what have you tried? - http://filipematias.info sense, purpose, direction Link to comment Share on other sites More sharing options...
CiberSkull Posted November 16, 2009 at 11:13 AM Author Report Share #296449 Posted November 16, 2009 at 11:13 AM thanks yoda.pt O meu código em comparação parece um atulho 😛 mas é assim q se aprende 🙂 Thanks to all Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now