搜索<div>中的单词并将文本放入标记<p> <! - p - > </p> </div>

时间:2014-05-22 13:24:23

标签: javascript jquery html

我想知道如何在我的div中添加标签

,例如“John”这个词。

我正在使用jquery选择器包含我的问题给这一个。附加它。 按照我的页面代码:

 <!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Beta style</title>
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>

    <div>John Resig</div>
    <div>George Martin</div>
    <div>Malcom John Sinclair</div>
    <div>J. Ohn</div>

    <script>
        $("div:contains('John')")*.append("<p></p>")*
    </script>

</body>
</html>

1 个答案:

答案 0 :(得分:2)

尝试使用jquery的.wrapInner()函数来完成任务,

$('div:contains("John")').wrapInner('<p></p>');

Demo demo-1