How change content value of pseudo :before element by Javascript

后端 未结 8 789
闹比i
闹比i 2020-11-29 23:34

I have the grap constructured by CSS, which is dynamically changes by JS. I show graph max value by pseudo element as:

.graph:before {
    content:\"\"; //va         


        
8条回答
  •  温柔的废话
    2020-11-30 00:21

    I hope the below snippet might help, you can specify the content value you want via JS using the CSS attr() function. Below you have two options: to use JavaScript or jQuery:

    jQuery:

    $('.graph').on('click', function () {
        //do something with the callback
        $(this).attr('data-before','anything'); //anything is the 'content' value
    });
    

    JavaScript:

    var graphElem = document.querySelector('.graph');
    graphElem.addEventListener('click', function (event) {
        event.target.setAttribute('data-before', 'anything');
    });
    

    CSS:

    .graph:before {
        content: attr(data-before); /* value that that refers to CSS 'content' */
        position:absolute;
        top: 0;
        left: 0;
    }
    

提交回复
热议问题