Is there a better way of inserting my element into text using Jquery? -


I wrote some jquery to solve this problem. Although this works, I think my jquery awesome & amp; I was hoping someone could tell me that I'm missing something and fixing my code to make me use some more awesome. Especially the string consensat I am doing (commented on seeing 'vomit') is lame, can anyone ignore it?

The problem is to get the HTML given below and the text has to move glucose in the braces a little further down with the anchor and change the anchor text from the words in the braces. It should be done normally (i.e. not hard-coded) because there are many.

I found that in order to anchor the text in this special way, really was not prepared for any jquery methods - I hope that 'I am wrong'

HTML given

  & lt; Tr & gt; & Lt; Td class = "german" & gt; Bin Ichin Ain {Glucose} Flidge & lt; / Td> & Lt; Td class = "english" & gt; I am a happy fly & lt; / Td> & Lt; TD & gt; & Lt; Div class = "lesson link" & gt; & Lt; A href = "http://www.german.org/happy" title = "happy" & gt; & Amp; Gt; & Amp; Gt; & Lt; / A & gt; & Lt; / Div & gt; & Lt; / TD & gt; & Lt; / TR & gt;  

desired HTML

  & lt; Tr & gt; & Lt; Td class = "german" & gt; Bin Ichi Anne & lt; A href = "http://www.german.org/happy" title = "happy"> gt; Gluclease & lt; / A & gt; Fliege & lt; / TD & gt; & Lt; Td class = "english" & gt; I am a happy fly & lt; / Td> & Lt; TD & gt; & Lt; Div class = "lesson link" & gt; & Lt; A href = "http://www.german.org/happy" title = "happy" & gt; & Amp; Gt; & Amp; Gt; & Lt; / A & gt; & Lt; / Div & gt; & Lt; / TD & gt; & Lt; / TR & gt;  

jquery

  $ (function () {$ ('td.german'). (Function () { Var Sentence = $ (this) .text (); // Squiglid Text Work Startbrace = Sentence. Index ('{'}; var Endbrace = Sentence. Endexoff ('}'); if (Startbrez === - 1 || EndBrace === -1) Return to; Var to Rehellus = Really Find Solace (Startbrace, Endbase + 1); // Find Anchor and replace it with Skigilded Works $ $ $ $ (this). ') First () .clone (); $ newAnchor.text (toReplace.slice (1, toreplace.length-1)); // Take the new anchor and put it in the main sentence // can I get the full-string from the DOM object? String Comkat Strange Word String Tunisian = '& lt; a href = "' + $ newAnchor [0] .href + '' 'Title = "' + $ newAnchor [0]. Title + '" & gt;' + $ new anchor [0] .text + '& lt; / a & gt;' var varicentence = replace the sentence (ToReplace, stringToInsert); $ (This) .html (newSentence);});});   

how a span , then setting html in the replaced text and copying the existing link in the call is? I think you will need to quote the braces because they are special in a regular expression - however, for JavaScript I might be wrong on this. If so, please let me know and I will update. Use closest and search with first to get the first link in the same line as the current table element.

  $ (function () {$ ('td.german'). (Function () {var link = $ (this) .clostest ('tr'). ('A: First ') Find clone (); var re = new RegExp (' \ {. * \} '); Var sentence = $ (this) .text (); var word = sentence. Match (again) [0]; Word = word substr (1, word.length-2); link.text (word); sentence = sentence. Place (then, ' 

If your link text contains the first word, then it will be very easy.

  $ (function () {$ ('td.german') .each (function () {var link = $ (this) .clostest ('tr'). ('A: First ') search .clone (); var sentence = $ (this) .text (). Replace (again,'  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -