最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Make bold text inside element (jQuery) - Stack Overflow

programmeradmin3浏览0评论

I'm trying to make text (fat) bold. Curretly I managed to do this whit code below:

Movie <b>fat</b> was been added

But this bold tags won't work. Now the question is how to enable html for only (fat), to be bold like this: Movie fat was been added

My code so far:

var boldy = (Movie[0].title)
var fat = boldy.bold()

$("div.container div:first").text("Movie " + (fat) + " was been added")

I also tried whit this:

$("div.container div:first").html("Hello <b>"+(boldy)+"</b>")

I'm trying to make text (fat) bold. Curretly I managed to do this whit code below:

Movie <b>fat</b> was been added

But this bold tags won't work. Now the question is how to enable html for only (fat), to be bold like this: Movie fat was been added

My code so far:

var boldy = (Movie[0].title)
var fat = boldy.bold()

$("div.container div:first").text("Movie " + (fat) + " was been added")

I also tried whit this:

$("div.container div:first").html("Hello <b>"+(boldy)+"</b>")
Share Improve this question asked Jun 3, 2017 at 22:31 SlasherSlasher 61810 silver badges32 bronze badges 5
  • The second example .html(...) works. Is Movie[0].title defined / empty string? – le_m Commented Jun 3, 2017 at 22:33
  • Second example returns empty string, yes and nothing happens. – Slasher Commented Jun 3, 2017 at 22:34
  • so... is Movie[0].title empty? What does console.log(boldy); print? – le_m Commented Jun 3, 2017 at 22:36
  • My first example applys <b> like it should. But html inside .text() is not working. – Slasher Commented Jun 3, 2017 at 22:37
  • 1 @Shasher It's not a bug, it's a feature - .text() does not render HTML - that's why you have .html(). – le_m Commented Jun 3, 2017 at 22:38
Add a ment  | 

4 Answers 4

Reset to default 4

var word ="fat"
$("div:first").html("Movie <b>"+word+"</b> was been added")
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div></div>

You can use the following:

$("div.container div:first").css("font-weight","Bold");

That will change the property font-weight from that div to bold.

@EDIT: You can make a div with an ID and set that ID on the jQuery mention:

$("div.container div:first div#text").css("font-weight","Bold");

var world = 'world';
$("div").html(`Hello <b>${world}</b>`);
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  Movie fat was been added
</div>

var html = $("div.container div:first").html;
$("div.container div:first").html(html.replace(/fat/gi, '<strong>$& </strong>'));
发布评论

评论列表(0)

  1. 暂无评论