Addressing pseudo-element first-letter in Javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fungii

    #1

    Addressing pseudo-element first-letter in Javascript

    Hello,

    I have a stylesheet that sets p:first-letter to a certain
    size and colour. I was playing around with Javascript to change
    paragraph stylesheets using an array like this:

    var paras = document.all.ta gs("P");

    Although I can change the paragraph colours using this
    array, the first letter of all the paragraphs remain the same
    colour. How do I refer to an array of the pseudo-element
    p:first-letter in the same way as the p tag?

    Thanks!

    Fungii
  • Martin Honnen

    #2
    Re: Addressing pseudo-element first-letter in Javascript



    Fungii wrote:

    [color=blue]
    > I have a stylesheet that sets p:first-letter to a certain
    > size and colour. I was playing around with Javascript to change
    > paragraph stylesheets using an array like this:
    >
    > var paras = document.all.ta gs("P");[/color]

    That doesn't change any stylesheets at all, you would need to use
    document.styleS heets
    to change style sheets.
    [color=blue]
    > Although I can change the paragraph colours using this
    > array, the first letter of all the paragraphs remain the same
    > colour. How do I refer to an array of the pseudo-element
    > p:first-letter in the same way as the p tag?[/color]

    I don't think the DOM provides access to pseudo elements, you can
    however (in IE/Win using
    document.styleS heets[index].rules[index]
    and in Mozilla using
    document.styleS heets[index].cssRules[index]
    ) access the rules in the style sheets thus if you already have a rule e.g.
    p:first-letter { font-weight: bold; }
    then you could change/add style declarations in that rule, once you have
    the rule e.g.
    rule.style.colo r = 'yellow';
    You can also add new rules if needed though the API in IE is different
    to the API in Mozilla (which follows the W3C DOM standard:
    <http://www.w3.org/TR/DOM-Level-2-Style/>
    ).


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Fungii

      #3
      Re: Addressing pseudo-element first-letter in Javascript

      Martin Honnen <mahotrash@yaho o.de> wrote in
      news:41eab59c$0 $27812$9b4e6d93 @newsread4.arco r-online.net:
      [color=blue]
      >
      > Fungii wrote:
      >
      >[color=green]
      >> I have a stylesheet that sets p:first-letter to a
      >> certain
      >> size and colour. I was playing around with Javascript to
      >> change paragraph stylesheets using an array like this:
      >>
      >> var paras = document.all.ta gs("P");[/color]
      >
      > That doesn't change any stylesheets at all, you would need
      > to use
      > document.styleS heets
      > to change style sheets.[/color]

      Yes I know, I was just pointing out how I created an array that
      I can then use to modify paragraph styles like this:

      paras(i).style. color = "#ffffff";
      [color=blue][color=green]
      >> Although I can change the paragraph colours using
      >> this
      >> array, the first letter of all the paragraphs remain the
      >> same colour. How do I refer to an array of the
      >> pseudo-element p:first-letter in the same way as the p
      >> tag?[/color]
      >
      > I don't think the DOM provides access to pseudo elements,[/color]

      Bogus, you would think if they are "there" you would be able to
      get to them somehow.
      [color=blue]
      > you can however (in IE/Win using
      > document.styleS heets[index].rules[index]
      > and in Mozilla using
      > document.styleS heets[index].cssRules[index]
      > ) access the rules in the style sheets thus if you already
      > have a rule e.g.
      > p:first-letter { font-weight: bold; }
      > then you could change/add style declarations in that rule,
      > once you have the rule e.g.
      > rule.style.colo r = 'yellow';[/color]

      Ok, I'll look into that method. How would I get the rule index
      for p:first-letter? Say I have one stylesheet in my page and the
      first entry in that stylesheet is the "p:first-letter" entry.
      Would that mean "document.style Sheets[1].cssRules[1]" is the way
      to refer to that object?
      [color=blue]
      > You can also add new rules if needed though the API in IE
      > is different to the API in Mozilla (which follows the W3C
      > DOM standard:
      > <http://www.w3.org/TR/DOM-Level-2-Style/> ).[/color]

      Alright, thanks your prompt help. I think this was the fastest
      reply I've ever received on usenet! :)

      Fungii

      Comment

      • Martin Honnen

        #4
        Re: Addressing pseudo-element first-letter in Javascript



        Fungii wrote:

        [color=blue]
        > How would I get the rule index
        > for p:first-letter? Say I have one stylesheet in my page and the
        > first entry in that stylesheet is the "p:first-letter" entry.
        > Would that mean "document.style Sheets[1].cssRules[1]" is the way
        > to refer to that object?[/color]

        Pretty much all indexing in JavaScript starts with index 0 so
        document.styleS heets[0]
        is the first style sheet and
        documeent.style Sheets[0].cssRules[0]
        the first rule in the first style sheet.
        You can also loop through the rules and check the selectorText property.

        --

        Martin Honnen
        http://JavaScript.FAQTs.com/

        Comment

        • Fungii

          #5
          Re: Addressing pseudo-element first-letter in Javascript

          Martin Honnen <mahotrash@yaho o.de> wrote in
          news:41eac3ce$0 $27822$9b4e6d93 @newsread4.arco r-online.net:
          [color=blue]
          > Pretty much all indexing in JavaScript starts with index 0
          > so
          > document.styleS heets[0]
          > is the first style sheet and
          > documeent.style Sheets[0].cssRules[0]
          > the first rule in the first style sheet.
          > You can also loop through the rules and check the
          > selectorText property.
          >[/color]

          Really? I thought Javascript was that weird language that began
          their indexes at 1 instead of 0. My bad.

          Alright thanks, that should do the trick! :)

          Fungii

          Comment

          • RobG

            #6
            Re: Addressing pseudo-element first-letter in Javascript

            Fungii wrote:[color=blue]
            > Martin Honnen <mahotrash@yaho o.de> wrote in
            > news:41eac3ce$0 $27822$9b4e6d93 @newsread4.arco r-online.net:
            >
            >[color=green]
            >>Pretty much all indexing in JavaScript starts with index 0
            >>so
            >> document.styleS heets[0]
            >>is the first style sheet and
            >> documeent.style Sheets[0].cssRules[0]
            >>the first rule in the first style sheet.
            >>You can also loop through the rules and check the
            >>selectorTex t property.[/color][/color]

            You are better off to loop through all the style sheet and then the
            styles. For the sake of development, print out the selectorText and
            check in various versions of IE.

            From memory, the selectorText for:

            .aClass { ... }

            will be ".aClass" (whereas the className will be "aClass") for W3C
            compliant browsers, but some (older) versions of IE prepend an
            asterisk: "*.aClass".

            The selector text for "p:first-letter" should be exactly that, but may
            be "*p:first-letter" in older IE.

            Incidentally, rather than:

            var paras = document.all.ta gs("P");

            Use (for cross-browser compatibility):

            if (document.getEl ementsByTagName ) {
            var paras = document.getEle mentsByTagName( "P");
            } else if (document.all) {
            var paras = document.all.ta gs("P");
            }

            --
            Rob

            Comment

            • RobB

              #7
              Re: Addressing pseudo-element first-letter in Javascript

              Fungii wrote:[color=blue]
              > Martin Honnen <mahotrash@yaho o.de> wrote in
              > news:41eac3ce$0 $27822$9b4e6d93 @newsread4.arco r-online.net:
              >[color=green]
              > > Pretty much all indexing in JavaScript starts with index 0
              > > so
              > > document.styleS heets[0]
              > > is the first style sheet and
              > > documeent.style Sheets[0].cssRules[0]
              > > the first rule in the first style sheet.
              > > You can also loop through the rules and check the
              > > selectorText property.
              > >[/color]
              >
              > Really? I thought Javascript was that weird language that began
              > their indexes at 1 instead of 0. My bad.
              >
              > Alright thanks, that should do the trick! :)
              >
              > Fungii[/color]

              Maybe this will be of some help. Big disappointment: Opera v.7 tossed
              in everything but the kitchen sink - and a document.styleS heets
              collection. Just fyi.

              <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
              "http://www.w3.org/TR/html4/strict.dtd">

              <html>
              <head>
              <title>untitled </title>
              <style type="text/css">

              body {
              height: 100%;
              font-size: 100%;
              }
              p {
              width: 400px;
              margin: 160px auto;
              font: normal 80% times, sans-serif;
              color: darkgreen;
              text-align: justify;
              padding: 6px;
              border: 1px #000 dashed;
              background: #fafffa;
              cursor: pointer;
              }
              p:first-letter {
              font: bolder 120% times, sans-serif;
              color: black;
              }

              </style>
              <script type="text/javascript">

              function modCSS(selector /* [attribute/value pairs] */)
              {
              if ('undefined' != typeof document.styleS heets)
              {
              var nsheets = document.styleS heets.length,
              re = new RegExp('\\b' + selector + '\\b', 'i'),
              SS,
              rtype,
              rule,
              rules,
              nrules;
              for (var nsheet = 0; nsheet < nsheets; ++nsheet)
              {
              SS = document.styleS heets.item(nshe et);
              rtype = ('undefined' != typeof SS.rules) ? 'rules' : 'cssRules';
              if ('undefined' != typeof SS[rtype])
              {
              rules = SS[rtype];
              nrules = rules.length;
              for (nrule = 0; nrule < nrules; ++nrule)
              {
              rule = rules.item(nrul e);
              if (re.test(rule.s electorText))
              {
              for (var a = 1; a < arguments.lengt h; a += 2)
              rule.style[arguments[a]] = arguments[a + 1];
              return;
              }
              }
              }
              }
              }
              }

              window.onload = function()
              {
              document.onclic k = function()
              {
              modCSS('p', 'color', 'black', 'fontSize', '90%');
              modCSS('p:first-letter','color' , 'green', 'fontSize', '160%');
              modCSS('body', 'background', 'darkolivegreen ');
              }
              }

              </script>
              </head>
              <body>
              <p title="click me">
              Lorem ipsum dolor sit amet, consectetur adipisicing elit,
              sed do eiusmod tempor incididunt ut labore et dolore magna
              aliqua. Ut enim ad minim veniam, quis nostrud exercitation
              ullamco laboris nisi ut aliquip ex ea commodo consequat.
              Duis aute irure dolor in reprehenderit in voluptate velit
              esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
              occaecat cupidatat non proident, sunt in culpa qui officia
              deserunt mollit anim id est laborum.
              </p>
              </body>
              </html>

              Comment

              Working...