Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.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 jQuery-更改具有相同类名的特定文本的字体大小_Javascript_Jquery_User Interface_Frontend - Fatal编程技术网

Javascript jQuery-更改具有相同类名的特定文本的字体大小

Javascript jQuery-更改具有相同类名的特定文本的字体大小,javascript,jquery,user-interface,frontend,Javascript,Jquery,User Interface,Frontend,我有两个具有相同类名的div: <button>Click to change font weight.</button> <div class="wt">Paragraph 1</p> <div class="wt">Paragraph 2</p> 您可以使用jQuery contains方法: $(".wt:contains(Paragraph 1)").css("font-weight","bold" ) 对于您

我有两个具有相同类名的div:

<button>Click to change font weight.</button>

<div class="wt">Paragraph 1</p>
<div class="wt">Paragraph 2</p>

您可以使用jQuery contains方法:

$(".wt:contains(Paragraph 1)").css("font-weight","bold" )

对于您的查询,我将添加类:

$('.wt:first').addClass('font-weight-bold');
如果您使用的是Bootstrap4,它将自动支持该类。否则,请在样式表中添加此规则:

.font-weight-bold {
  font-weight: bold;
}
如果您的段落不是DOM树中的第一个并在查找文本,则按照其他答案中的建议使用包含选择器:

$('.wt:contains("Paragraph 1")').addClass('font-weight-bold');
最后一件事,我忘了提到可以使用toggleClass在单击按钮时切换更改

$('.wt:first').toggleClass('font-weight-bold');
您可以使用jQuery的选择器

我添加了一些附加功能,以便您可以在字体大小之间切换。值得注意的是,使用jQuery调用.css'font-weight'将不会返回字符串值,如normal或bold;相反,调用此函数将检索数字“400”==“正常”、“700”==“粗体”等

$button.clickfunction{ 让par1=$.wt:包含“段落1”; //在字体权重之间切换 par1.css “字体大小”, ['normal','400'].indexOfpar1.css'font-weight'==-1?'normal':'bold' ; //或者,如果不希望切换div,请取消注释并使用此选项 //par1.css'font-weight','bold'; }; 单击以更改字体大小。 第1款
第2段加粗:啊,我错了,你使用$this选择器让我不知所措。请用代码片段的文本解释编辑你的答案,并解释它是如何回答问题的
$('.wt:first').toggleClass('font-weight-bold');
 $("button").click(function(){
   $(".wt").each(function(){
     if($(this).text() == "Paragraph 1")
     $(this).css("font-weight","bold");
   });
 });