如何使用jQuery使所有元素的第一个字变粗

如何使用jQuery使所有元素的第一个字变粗

在这篇文章中,我们将使用jquery使所有元素的第一个字变粗。为了使第一个词变粗,我们使用html()和text()方法。

html()方法: jQuery中的html()方法是用来设置或返回选定元素的innerHTML内容。

语法:它设置匹配元素的内容。

$(selector).html(content)

text() 方法:该方法用于设置或返回元素的文本内容。在设置内容时,它覆盖了所有匹配元素的内容。text method()返回的内容被用来返回所有匹配元素的文本内容。

语法:它设置文本内容。

$(selector).text(content)

示例:

<!DOCTYPE html>
<html lang="en">
  
<head>
    <title>
        How to make first word bold
        of all elements using jQuery?
    </title>
  
    <!-- Import jQuery cdn library -->
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
  
    <script>
        (document).ready(function () {
            ("p").each(function () {
                var txt = (this);
                txt.html(txt.text()
                .replace(/(^w+)/, '<strong>1</strong>'));
            });
        });
    </script>
</head>
  
<body style="text-align: center;">
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
  
    <h3>
        How to make first word bold of
        all elements using jQuery?
    </h3>
      
    <p>GFG is a computer science portal</p>
   
    <p>Computer Science Portal</p>
</body>
  
</html>

输出:

如何使用jQuery使所有元素的第一个字变粗?

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程