简体   繁体   中英

Search a word within a <div> and put text inside a tag <p> </ p>

I wonder how can I be adding a tag

for example, the word "John" in my div.

I am using jquery selector contains my problem to give this one. Append it. Follows the code of my page:

 <!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>

Try to use jquery's .wrapInner() function to accomplish your task,

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

Demo , demo-1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM