Alcanzar y manipular pseudo-elementos CSS con JavaScript

9/Jul/2015 88

Los pseudo-elementos (y pseudo-clases) son selectores CSS virtuales que no existen esplícitamente en el árbol de DOM. Pero aparte de esa característica, puede ser manipulado con propiedades CSS como cualquier otra, a excepción que agrega una nueva propiedad content la que permite ingresar texto al pseudo-elemento.

Como estos selectores no existen en el árbol del documento, cómo alcanzarlos y manipularlos con JavaScript? Afortunadamente es posible con el método getComputedStyle():

HTML:

<p> Mundo</p>

CSS:

p:before {
  content: "Hola";
}

JS:

var elem = document.querySelectorAll("p");
if( elem.length ) {
  [].forEach.call( elem, function(e) {
    var pseudoContent = window.getComputedStyle(e,':before').content;
    document.body.innerHTML += "Pseudo-element content: " + pseudoContent;
  });
}

JS Bin on jsbin.com

También puedes obtener otras propiedades CSS aplicadas, por ejemplo, el color de texto:

JS:

var pseudoColor = window.getComputedStyle(e,':before').getPropertyValue('color');

JS Bin on jsbin.com

Link:

Comentarios

  1. Bukjyn [#]

    chewing cialis tablets – cialis generico super active 20 mg cialis 120

  2. hydroxychloroquine vidal [#]

    hydroxychloroquinine https://hydroxychloroquinex.com/# – hydroxychloride medicine hydrochloroquin

  3. Pgpmyc [#]

    cymbalta capsule 40 mg – cymbalta tablets cost cymbalta 20

  4. Zddwxm [#]

    cymbalta generic usa – duloxetine 15 mg duloxetine tablets price

  5. Qqlqjy [#]

    synthroid 10 mcg – synthroid prescription cost where to buy levothyroxine online

  6. Dykovb [#]

    112 mg synthroid – buy levothyroxine 0.0125 online synthroid 150 mg

  7. hydrochlorothiazide 12.5 mg [#]

    does erectile dysfunction affect desire https://plaquenilx.com/ is erectile dysfunction normal

  8. Vsbjsc [#]

    cvs amoxicillin price no insurance – http://genamoxi.com/# walmart amoxicillin price

  9. Anyoew [#]

    price of amoxicillin without insurance – buy amoxicilin 500 mg online mexico buy amoxicillin 500 mg online mexico

  10. Fsynap [#]

    buy vardenafil with paypal – buy generic vardenafil from india online vardenafil cost

  11. Jkmfjg [#]

    generic viagra cialis vardenafil cheap – buy vardenafil online uk vardenafil coupons

  12. order tadalafil [#]

    tadalafil 40 mg daily https://pulmoprestadalafil.com/ tadalafil daily use tadalafil 30 mg

  13. tadalafil [#]

    buy tadalafil https://superactivetadalafil.com/ tadalafil 30 mg tadalis sx

  14. tadalafil daily use [#]

    tadalafil pills 20mg https://nextadalafil.com/ 40 mg tadalafil order tadalafil

  15. Ylicnt [#]

    what is generic vardenafil – variked.com buy vardenafil australia

  16. Bthebz [#]

    levitra 10mg – generic vardenafil suppliers vardenafil 20 mg

  17. tadalafil 60 mg for sale [#]

    generic tadalafil https://tadalafilgenc.com/ tadalafil 40 tadalis sx

  18. Nyzgsk [#]

    canadian drug pharmacy – online pet pharmacy online canadian pharmacy review

  19. Piynpr [#]

    canadian pharmacy viagra reviews – canadian pharmacy world coupon canadian pharmacy meds

  20. cialis [#]

    buy tadalafil cialis generic tadalafil research powder

  21. Ijyzsn [#]

    canada cialis pills – buy cialis pay with paypal how to buy cialis in usa

  22. Vudiwl [#]

    generic tadalafil usa pharmacy – canadian cialis cheap generic cialis from india

  23. tadalafil brands [#]

    side effects for tadalafil canada generic tadalafil tadalafil powder suppliers

  24. Ytxmkt [#]

    how to get a prescription for viagra – erectylepls.com viagra sildenafil 150mg

  25. Zrheiq [#]

    can you buy viagra in australia over the counter – over the counter viagra india 150 mg generic viagra

  26. Rirfyo [#]

    buy vardenafil vardenafil – buy vardenafil overnight shipping buy vardenafil germany

  27. Iepvne [#]

    discount vardenafil online – order vardenafil generic vardenafil cost

  28. Memufx [#]

    how to use vardenafil – vardenafil 5 mg want to buy vardenafil

  29. Xqzens [#]

    cialis vardenafil online – buy cheap vardenafil levitra pill

  30. Lourtz [#]

    ivermectin 0.1 – where to buy ivermectin pills ivermectin cream canada cost

  31. Qfgqgx [#]

    stromectol pills canada – ivermectina online stromectol 3mg online

  32. Xxihid [#]

    where can i buy viagra for women – purchase viagra with paypal us over the counter viagra

  33. Xpqywg [#]

    viagra online pharmacy canada – uk viagra buy where to get female viagra uk

  34. Iypwub [#]

    best us price tadalafil – cheap online cialis generic cialis soft gel

  35. Xjoske [#]

    cialis order usa – cheapest tadalafil prices canadadrugpharmacy

  36. www.pharmaceptica.com [#]

    prescription tadalafil online https://pharmaceptica.com/

  37. Salina [#]

    Excellent blog here! Also your web site loads up very fast!
    What web host are you using? Can I get your
    affiliate link to your host? I wish my site loaded up
    as fast as yours lol

Deja tu Comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

CSSLab