In the block with id = "words", select the text hello world and add it to the block with id = "insert" in <span> instead of the string "abracadabra" here are the symbols

<html> <div id="words"> <p>что-то там...</p> <p>hello world</p> <!--вот это выбираем---> </div> <div id="insert"> <p>text <span>1</span> </p> <p>text <span>2</span> </p> <p>text 3 <span>abracadabra<!-- вот сюда вставляем--></span> </p> </html> <script> var y = $('#words').has(':contains("hello world")'); //выбрал hello world var z = $('div#insert').children().eq(3).has('span:contains("abracadabra")').append(y) //в итоге хрень получается </script> 
  • And the question is what? Issue the code on jsfiddle. - Alexey Anufriev
  • And the question is what? What if my request is wrong and it needs to be rewritten somehow - cheburashkarf

3 answers 3

Try this, it seems to work, but I want to note one nuance: the script is oriented by indexes, if you add the span tag to the example at the beginning, the index number will change.

 var s = $('#words p').eq(1).html(); // ищем строку hello world $("#insert span").eq(2).html(s); // заменяем текст в span 

    See an example

     // Получаем дочерний(-ие) элемент(-ы) из #words, содержащие строку 'hello world' var results = getElementByContent('#words', '', 'hello world'); // Если найден хотя бы один такой элемент if (results.length > 0) { // Получаем дочерний(-ие) span из #insert, содержащие строку 'abracadabra' var target = getElementByContent('#insert', 'span', 'abracadabra'); // Если найден хотя бы один такой элемент if (target.length > 0) { // Заменяем все вхождения 'abracadabra' на 'hello world' внутри этих элементов target.text(target.text().replace(/abracadabra/g, 'hello world')); } } /** * @text - искомая строка * @haystack - селектор тэга-обертки * @specific - селектор дочернего тэга, по умолчанию '*', т.е. любой дочерний элемент */ function getElementByContent (haystack, specific, text) { specific = (!specific || typeof specific !== 'string') ? '*' : specific; // Выбрать все дочерние элементы haystack // Оставить из них только specific, содержащие text return $(haystack).find('*').filter(specific + ':contains("'+text+'")'); } 

      What contains still, I would do this:

       $('.words').each(function(ind){ var txt = $(this).text(); if (txt=='hellow world') { $('.text').eq(ind).text(txt); } }); 

      Sorry I write from the phone, provided that keywords are given a class .words and where they need to enter the class .text