0

我希望伯恩茅斯这个词是红色的,但其余的词要从 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

4 回答 4

4

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

HTML:

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

CSS:

.red
{
color:#FF0000;
}
于 2013-02-12T15:55:01.937 回答
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(

于 2013-02-12T15:55:40.677 回答
0

尝试如下..它会工作...

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

msg = "Tomorrow's lunch deals in <p style='color:red'>Bournemouth</p>";
$('#time p').html(msg);
于 2013-02-12T15:58:53.927 回答
0

将 span 添加到文本 msg 然后将其附加为 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;}
于 2013-02-12T16:02:06.913 回答