search

Home  >  Q&A  >  body text

javascript - Set inline style attributes.

For example<p style="color:red">Set the attribute of p tag<p>
Why use p.style.color?
Why not style.color?
style can be regarded as an object, and color is an attribute. Just use style.color directly.

黄舟黄舟2709 days ago766

reply all(6)I'll reply

  • PHP中文网

    PHP中文网2017-06-26 10:58:59

    No, Style is an attribute of the HTML element, and its value will be parsed by the browser into an inline style.

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-06-26 10:58:59

    Style itself is an attribute owned by the dom element p, which needs to be accessed through the current element.
    Access style.color directly. If the style of which element you want to access cannot be parsed, an error will be reported

    reply
    0
  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-26 10:58:59

    <p style='color:#fff;font-size:16px'>

    style belongs to the inline style of p items and elements. It sets the attributes of p. The color and font-size in the attributes are equivalent to the attribute values.

    reply
    0
  • 怪我咯

    怪我咯2017-06-26 10:58:59

    var p = {
      style: {
        color: '#fff'
      }
    }
    console.log(p.style.color) // #fff
    
    style.color = 'red' // style is not defined

    reply
    0
  • 三叔

    三叔2017-06-26 10:58:59

    Style is an attribute and must be behind the object. Just think about it, if you directly style.color, does that change the color of everything? Obviously this is not true.

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-06-26 10:58:59

    If you want to set a color style, you should specify whose color to set, right?

    reply
    0
  • Cancelreply