Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 字符串的粗体部分/向变量添加样式_Javascript_Jquery_Css_Variables_Substring - Fatal编程技术网

Javascript 字符串的粗体部分/向变量添加样式

Javascript 字符串的粗体部分/向变量添加样式,javascript,jquery,css,variables,substring,Javascript,Jquery,Css,Variables,Substring,我只需要在这个字符串中加粗日期 <div class="foo"><p> Click here now and get by January 1st </p></div> 但是我不能加粗我创建的子字符串/变量。我希望这样的事情能奏效: $(justTheDate).css("font-weight","700"); 这可能吗?假设日期在末尾,您可以使用jquery(或常规javascript)创建一个span,并使用append或appen

我只需要在这个字符串中加粗日期

 <div class="foo"><p> Click here now and get by January 1st </p></div>
但是我不能加粗我创建的子字符串/变量。我希望这样的事情能奏效:

 $(justTheDate).css("font-weight","700");

这可能吗?

假设日期在末尾,您可以使用jquery(或常规javascript)创建一个span,并使用append或appendChild将其添加到p标记中

var fullMessage =  $('.foo p')
var justTheDate = fullMessage.substring(fullMessage.text().indexOf('get by') + 7);
fullMessage.append('<span class="date">' + justTheDate + '</span>');
var fullMessage=$('.foo p')
var justTheDate=fullMessage.substring(fullMessage.text().indexOf('get by')+7);
fullMessage.append(“”+justTheDate+“”);
将它放在一个span中,并使用一个类来处理该部分的css

<style>
    .date {
        font-weight: 700;
    }
</style>
<div class="foo"><p> Click here now and get by <span class="date">January 1st</span></p></div>

.日期{
字号:700;
}
现在点击这里,1月1日前到达


我希望这能帮助您:

var element = $('.foo p'),
    fullMessage =  element.text(),
    justTheDate = fullMessage.substring(fullMessage.indexOf('get by') + 7),
    boldIt = '<span style="font-weight: 700">' + justTheDate + '</span>',
    newHtml = fullMessage.replace(justTheDate, boldIt);

    element.html(newHtml)
var元素=$('.foo p'),
fullMessage=element.text(),
justTheDate=fullMessage.substring(fullMessage.indexOf('get by')+7),
粗体=“”+正体日期+“”,
newHtml=fullMessage.replace(justTheDate,boldIt);
html(newHtml)

不知道为什么不能简单地将日期部分包装在一个强标记中

如果日期是动态填充的,比如通过变量填充的,那么您可以在p标记中有一个span标记的占位符,其中包含一个类

作为

将日期值注入该标记,然后简单地将css应用于该标记


span.date{font-weight:600;}

OP询问如何添加它。。。。不直接在HTML中添加它。相信我,如果可以的话,我会这么做的!数据正以这种方式返回给我。从您提出问题的方式来看,不清楚您正在处理一个已经呈现的html页面。
var element = $('.foo p'),
    fullMessage =  element.text(),
    justTheDate = fullMessage.substring(fullMessage.indexOf('get by') + 7),
    boldIt = '<span style="font-weight: 700">' + justTheDate + '</span>',
    newHtml = fullMessage.replace(justTheDate, boldIt);

    element.html(newHtml)