简体   繁体   English

如何设置我在 JavaScript 中加粗的元素?

[英]How can style the element that I bold in JavaScript?

Problem on how to style the append element in javascript.关于如何在 javascript 中设置 append 元素样式的问题。 How can style the elemnt that i bold in javascript?如何设置我在 javascript 中加粗的元素? try to style it in the javascript but failed to.尝试在 javascript 中对其进行样式设置,但未能成功。 Can someone help me?有人能帮我吗? How can style this part?这部分的样式如何?

                            let contents = document.createElement('div');
                            contents.innerHTML = **val1.mail_no + ' - ' + val1.mail_subject;**
                          

you can do it by updating style attribute of contents :您可以通过更新contentsstyle属性来做到这一点:

contents.style.fontWeight='bold'

document.getElementById( agency_${agoptions.ag_id}_${agoptions.ad_id} ).appendChild(contents).style.fontWeight = "900"; document.getElementById( agency_${agoptions.ag_id}_${agoptions.ad_id} ).appendChild(contents).style.fontWeight = "900";

Here are couple of ways you can try您可以尝试以下几种方法

 document.getElementById('one').innerHTML = '<b> Show as bold </b>'; document.getElementById('two').innerHTML = '<span style="font-weight: bold;"> bold again </span>';
 <div id="one">x</div> <div id="two">y</div>

first of all try to avoid using javascript to style elements, but if you have to here is how to do it首先尽量避免使用 javascript 来设置元素的样式,但是如果你必须在这里是怎么做的

  1. method 1: declare a class in css方法一:在 css 中声明一个 class

    .bold{font-weight:bold;} .bold{字体粗体:粗体;}

then add然后加

document.getElementById("id of element").classList.add("bold");
  1. method 2:方法二:

    document.getElementById("id of element").style.fontWeight="bold"; document.getElementById("元素的id").style.fontWeight="bold";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM