我怎样才能在段落中加粗前两个单词?

时间:2017-12-15 13:22:40

标签: javascript jquery html css angularjs

我怎样才能在一个段落中粗体加粗前两个单词?

我试图在段落中加粗前两个单词,我该怎么办?那个

使用HTML Javascript Css Jquery AngularJs

1 个答案:

答案 0 :(得分:1)

您可以通过以下方式实现此目的:

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>
    <p class="boldTwoWord">How can I Bold First two words bold in a paragraph Dynamically?</p>
    <br/>
    <p class="boldTwoWord">I am trying to bold first two words in the Paragraph what can i do ? for that</p>    
</body>
<script type="text/javascript">
    var elms = $(".boldTwoWord");
    for (var i = 0; i < elms.length; i++) {
        str = elms[i].innerText;
        var ret = str.split(" ");
        var str1 = ret[0];
        var str2 = ret[1];

        s1 = str.replace(str1, '<strong>$&</strong>');
        s2 = s1.replace(str2, '<strong>$&</strong>');
        elms[i].innerHTML = s2;
    }
</script>
</html>