admin管理员组文章数量:1323335
To simplify, I have a label set in my html as:
<label id ="lblApptDateTime" >Appointment</label>
On an event I am changing the lext of lblApptDateTime to Appointment with an asterisk. I want the asterisk to be red color and in bold font. How do i achieve that ?
Here's what I am changing the text on the event:
$("#lblApptDateTime").html("Appointment" + "*");
Need to change the color of asterisk to red and in bold font.
To simplify, I have a label set in my html as:
<label id ="lblApptDateTime" >Appointment</label>
On an event I am changing the lext of lblApptDateTime to Appointment with an asterisk. I want the asterisk to be red color and in bold font. How do i achieve that ?
Here's what I am changing the text on the event:
$("#lblApptDateTime").html("Appointment" + "*");
Need to change the color of asterisk to red and in bold font.
Share Improve this question asked Sep 15, 2015 at 19:22 HereToLearn_HereToLearn_ 1,1804 gold badges26 silver badges49 bronze badges4 Answers
Reset to default 4You can add the asterisk in an :after
pseudo-element. This way you can style it to your liking, apart from the text.
.asterisk:after {
content: '*';
color: red;
}
In your JavaScript, instead of adding the asterisk inside the label, you just add the class asterisk
to it:
$("#lblApptDateTime").html("Appointment").addClass("asterisk");
You can also use toggleClass()
instead of addClass()
if the asterisk should be toggle-able.
Here's a working fiddle. Since I didn't have any other event available, I used hover
.
Wrap it in a span and use a class
$("#lblApptDateTime").html('Appointment' + '<span class="red">*</span>');
.red {
color: red;
font-weight: bold
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<label id="lblApptDateTime">Appointment</label>
HTML:
$("#lblApptDateTime").html("Appointment" + "<span class="red">*</span>");
CSS:
span.red {
color: red;
font-weight: bold;
}
You'll need to put it in a new styled element like a span
, which you can do with append
:
$("#lblApptDateTime").append($("<span>").css("color", "red").text("*"));
Or addClass()
instead of using css()
and style it appropriately.
本文标签: javascriptJquery change text and css for part of textStack Overflow
版权声明:本文标题:javascript - Jquery change text and css for part of text - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742135993a2422371.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论