我有一个消息输出,我想有一个单词橙色,其余正常

时间:2013-02-12 15:52:28

标签: javascript jquery html css

我希望伯恩茅斯这个词是红色的,但其余的话要从p造型继承。

<script type="text/javascript">
    $(document).ready(function() {
        var now = new Date();
        var hours = now.getHours();
        var msg;
        if (hours >= 0 && hours <= 12) msg = "Today's lunch deals in Bournemouth";
        else if (hours >= 13 && hours <= 19) msg = "Tonight's dinner deals in Bournemouth";
        else msg = "Tomorrow's lunch deals in Bournemouth";
        $('#time p').text(msg);
    });
</script>

<div id="time" style="position:absolute; width:100%; padding-top:90px; text-align:center; font-family: Arial, Helvetica, sans-serif; font-weight:bold; color:#FFF; font-size:17px;">
    <p></p> 
</div>

我需要将某些标签用于jQuery颜色吗?我试过HTML,但这只是打印出代码。

4 个答案:

答案 0 :(得分:4)

为了设置单行或单词的样式,您可以使用span标记。

HTML:

<p>Lorem ipsum dolor sit <span class="red">amet</span></p>

CSS:

.red
{
color:#FF0000;
}

答案 1 :(得分:1)

你不用jQuery给某些东西着色(或者至少不是直接的,你可以用它来修改CSS)

尝试这样的事情:

if (hours >= 0 && hours <= 12) msg = "Today's lunch deals in <span class='red'>Bournemouth</span>";
  else if (hours >= 13 && hours <= 19) msg = "Tonight's dinner deals in <span class='red'>Bournemouth</span>";
  else msg = "Tomorrow's lunch deals in <span class='red'>Bournemouth</span>";

然后在你的html文件中:

<style>
.red {
  color: red;
}
</style>

您还需要将.text(换成.html(

答案 2 :(得分:0)

尝试如下......它会起作用......

好的,然后使用下面的<p> ..

msg = "Tomorrow's lunch deals in <p style='color:red'>Bournemouth</p>";
$('#time p').html(msg);

答案 3 :(得分:0)

在文本消息中添加span,然后将其添加为html

 $(document).ready(function() {
  var now = new Date();
  var hours = now.getHours();
  var msg = " deals in <span class='highlight'>Bournemouth</span>";
  if (hours >= 0 && hours <= 12) msg = "Today's lunch"+ msg;
  else if (hours >= 13 && hours <= 19) msg = "Tonight's dinner"+ msg;
  else msg = "Tomorrow's lunch" + msg;
  $('#time p').html(msg);
});

为你的css添加一个高亮课程:

.highlight{color:red;}