最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Changing CSS content property dynamically - Stack Overflow

programmeradmin3浏览0评论

A friend of mine is renting a webshop from a pany. He is able to choose from different templates, and has the ability to override the predefined CSS and add javascript snippets. He asked me to help her making several changes, but there is something I cannot deal with: the add to cart button. In the below CSS there is a "content" property that holds a predefined value. I would like to change the value of this property dinamically, based on the HTML lang attribute. Do you have any idea how could I achieve this? (I know how to get the value of the lang attribute. My problem is changing the value of the "content" property)

#add_to_cart:before {
    display: block;
    font-family: 'sosa';
    font-size: 35px;
    content: "Ä";
    padding-right: 5px;
    font-weight: 400;
    width: 71px;
    height: 71px;
    line-height: 65px;
    text-align: center;
    text-indent: 0;
    text-shadow: none;
    color: #fff;
}

A friend of mine is renting a webshop from a pany. He is able to choose from different templates, and has the ability to override the predefined CSS and add javascript snippets. He asked me to help her making several changes, but there is something I cannot deal with: the add to cart button. In the below CSS there is a "content" property that holds a predefined value. I would like to change the value of this property dinamically, based on the HTML lang attribute. Do you have any idea how could I achieve this? (I know how to get the value of the lang attribute. My problem is changing the value of the "content" property)

#add_to_cart:before {
    display: block;
    font-family: 'sosa';
    font-size: 35px;
    content: "Ä";
    padding-right: 5px;
    font-weight: 400;
    width: 71px;
    height: 71px;
    line-height: 65px;
    text-align: center;
    text-indent: 0;
    text-shadow: none;
    color: #fff;
}
Share Improve this question edited Dec 2, 2014 at 21:08 Barnabás Nagy asked Dec 2, 2014 at 21:01 Barnabás NagyBarnabás Nagy 831 gold badge1 silver badge8 bronze badges 3
  • Refer to this answer, it's only possible using workarounds. – Azeirah Commented Dec 2, 2014 at 21:15
  • Thanks! I was able to solve it :) – Barnabás Nagy Commented Dec 2, 2014 at 21:47
  • I changed your profile image, @BarnabásNagy; that one wasn't really appropriate here. Thanks! – Andrew Barber Commented Dec 2, 2014 at 22:09
Add a ment  | 

2 Answers 2

Reset to default 13

Try this:

HTML:

<html lang="en">
...
<div id="add_to_cart" data-content="">example</div>

CSS:

#add_to_cart:before {
    display: block;
    font-size: 35px;
    content: attr(data-content);
    padding-right: 5px;
    font-weight: 400;
    width: 71px;
    height: 71px;
    line-height: 65px;
    text-align: center;
    text-indent: 0;
    text-shadow: none;
    color: red;
}

JS:

$('#add_to_cart').attr('data-content', (document.documentElement.lang == 'en' ? "x" : "y"));

You'll see that the character before 'example' changes when lang attr of <html> is changed.

Write a Javascript function that updates the CSS. Javascript can access the HTML attribute.

document.documentElement.lang

发布评论

评论列表(0)

  1. 暂无评论