dsfdfd1211 2019-03-31 10:34
浏览 64

如何使用CSS生成的动态内容可用于CSS?

I am using WordPress. A plugin has php code segments that, once loaded in html, create strings dependant on user choices. I would like to use such strings in CSS. I have already tried Right-way-for-pass-variable-php-to-css-in-wordpress, but including a CSS stylesheet with PHP code as style.php does not work (maybe something from the Wordpress theme is interfering).

Is there another way to do this?

  • 写回答

1条回答 默认 最新

  • doumeng4400 2019-03-31 11:13
    关注

    Here is a working solution:

    Put the php code that produces the string into a php file of the page that is always loaded (e. g. the header.php). Assign an ID.

    <div id="myid"><?php produces-a-string ?></div>
    

    Make the html string it produces invisible to the ordinary user with CSS:

     #myid {
    display: none;
    }
    

    Grab the loaded string with JavaScript to create a CSS variable:

    var grab = document.getElementById("myid");
    var text = grab.innerText;
    grab.setProperty("--grabbed", text);
    

    Caution: The php content must be loaded before JavaScript, else the corresponding JavaScript variable will be null. If you put the php segment in the header, do it before it calls JavaScript. If you put it below, or in another part of the page, place the above code in an event listener "load".

    This creates the CSS code:

    #myid {
    --grabbed: string-produced;
    }
    

    The string can then be used by other CSS attributes:

    main {
    attribute: var(--grabbed);
    }
    

    which acts like:

    main {
    attribute: string-produced;
    }
    

    Caution: If you want to use the produced string with CSS attribute "content", creating a CSS variable does not work, as content does not accept var() as value.

    But I found a hack, as content accepts attr() as value. Change JavaScript to:

    var grab = document.getElementById("myid");
    var text = grab.innerText;
    grab.setAttribute("title", text);
    

    This assigns the HTML global attribute (allowed for any HTML element) "title" to div myid, giving it the value of the php string. You can chose any global attribute.

    This creates the HTML code:

    <div id="myid" title="string-produced"></div>
    

    Caution: In JavaScript, you have to assign the title attribute to the element with the class or id you want to add a "content" attribute to:

    document.getElementBy(id-where-content-is-added).setAttribute("title", text);
    

    This makes the following possible:

    #id-where-content-is-added {
    content: attr(title);
    }
    

    which acts like:

    #id-where-content-is-added {
    content: string-produced;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导