Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   замена текста html тегами (https://javascript.ru/forum/misc/67606-zamena-teksta-html-tegami.html)

Фридрих 27.02.2017 04:56

замена текста html тегами
 
возникла труднось обернуть искомый текст в html теги
вот такой вот поиск у меня
$('.article p, .article h2').each(function() {
        var text = $(this).text().replace(/nor/ig, "$&");
        $(this).text(text);
    })

мне нужно както обернуть $& тегами <span style="color.red"></span>, чтоб найденный текст выделялся...
за ранее благодарен

laimas 27.02.2017 05:43

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script> 
$(function() {
$('.article p, .article h2').each(function() {
        console.log($(this).text().match(/nor/ig))
        var text = $(this).text().replace(/nor/ig, '<span style="color:red">$&</span>');
        $(this).html(text);
    })
});
</script> 
</head>
<body>
<div class="article">
    <h2>qwernorqwerty</h2>
    <p>qwernOrqwerty</p>
</div>
</body>
</html>


Часовой пояс GMT +3, время: 09:06.