Javascript 我有一个消息输出,我想有一个词橙色和其他正常
我希望Bournemouth这个词是红色的,但是其余的词是从p样式继承的Javascript 我有一个消息输出,我想有一个词橙色和其他正常,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我希望Bournemouth这个词是红色的,但是其余的词是从p样式继承的 <script type="text/javascript"> $(document).ready(function() { var now = new Date(); var hours = now.getHours(); var msg; if (hours >= 0 && hours <= 12) msg
<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>
$(文档).ready(函数(){
var now=新日期();
var hours=now.getHours();
var-msg;
如果(hours>=0&&hours=13&&hours要设置单行或单词的样式,可以使用span
标记
HTML:
要设置单行或单词的样式,可以使用span
标记
HTML:
您不需要使用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>";
如果(小时数>=0&&hours=13&&hours您没有使用jQuery为某些内容着色(或者至少不直接使用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>";
如果(小时数>=0&&hours=13&&hours尝试下面的方法..它会工作的
确定,然后使用如下所示的
msg = "Tomorrow's lunch deals in <p style='color:red'>Bournemouth</p>";
$('#time p').html(msg);
msg=“明天的午餐以伯恩茅斯
”的形式出售;
$('timep').html(msg);
试试下面的方法..它会起作用的
确定,然后使用如下所示的
msg = "Tomorrow's lunch deals in <p style='color:red'>Bournemouth</p>";
$('#time p').html(msg);
msg=“明天的午餐以伯恩茅斯
”的形式出售;
$('timep').html(msg);
将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);
});
将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);
});
当然是span标记。此外,内联样式是个难题。我所键入的是-尽管他需要将span添加到脚本中。您好,我尝试过,但文本是从JQuery中的行中动态提取的。@scott-将您的.text更改为.html,span标记环绕在单词周围。大家好,非常感谢您-就像Mark所说的,我更改了文本。文本转换为.html。然后像大家建议的那样使用span类对输出进行取舍。效果很好,非常感谢!span标记,当然。此外,内联样式是最糟糕的。我键入的是-尽管他需要将span添加到脚本中。您好,我试过了,但文本是从JQuery中的行中动态提取的。@scott-更改您的.text to.html,其中span标记环绕在单词周围。大家好,非常感谢-正如Mark所说,我将.text更改为.html。然后按照大家的建议使用span类对输出进行循环。非常有用,非常感谢!将.text()
替换为.html()
将使您能够在msg
变量中使用HTML。然后您将能够使用类或ID包装特定单词。将.text()
替换为.HTML()
将使您能够在msg
变量中使用HTML。然后您将能够使用类或ID包装特定的单词。字体元素已被高度弃用,HTML5不再支持它!完全不建议使用!字体元素已被高度弃用,HTML5不再支持它!根本不建议使用!我没有看到r伊森重复了最后一部分,所以有一个轻微的重构。我认为没有理由重复最后一部分,所以有一个轻微的重构。
.highlight{color:red;}