简体   繁体   English

jQuery .css()不起作用

[英]jQuery .css() doesn't work

I'm trying to change inline CSS using jQuery on my website. 我正在尝试在我的网站上使用jQuery更改内联CSS。 So far I have tried the following code: 到目前为止,我已经尝试了以下代码:

<script type="text/javascript">
jQuery(".shareaholic-share-button-container").css("background", "rgba(0,0,0,0)");
jQuery(".share-button-counter").css("background", "rgba(0,0,0,0)");
</script>

I have also tried: 我也尝试过:

<script type="text/javascript">
jQuery(document).ready(function(){
jQuery(init);
function init() {
jQuery(".shareaholic-share-button-container").css("background", "rgba(0,0,0,0)");
jQuery(".share-button-counter").css("background", "rgba(0,0,0,0)");
}
});
</script>

And

<script type="text/javascript">
jQuery(document).ready(function(){
jQuery(".shareaholic-share-button-container").css("background", "rgba(0,0,0,0)");
jQuery(".share-button-counter").css("background", "rgba(0,0,0,0)");
});
</script>

However, nothing is working. 但是,没有任何效果。 Interestingly the code works when Cloudflare's development mode is turned on, but when it is turned off the code doesn't work (even after purging the Cloudflare cache and disabling minification / rocket loader). 有趣的是,当打开Cloudflare的开发模式时,代码可以工作,但是当关闭Cloudflare的开发模式时,代码不起作用(即使清除了Cloudflare缓存并禁用了缩小/火箭加载程序)。

Appreciate all help! 感谢所有帮助!

Edit: the whole point of this code is to replace the !important inline CSS and change the background to be completely transparent, ie zero opacity. 编辑:此代码的全部重点是替换!important内联CSS并将背景更改为完全透明,即不透明度为零。 So rgba(0,0,0,0) is correct and intended. 因此rgba(0,0,0,0)是正确的和预期的。

The purpose of the code is to remove !important 'background' inline style, see: 该代码的目的是删除!important'background'内联样式,请参见:

<div class="shareaholic-share-button-container" ng-click="sb.click()" style="color:#000000 !important;
                      background:#fafafa !important; // the purpose is to remove this line
                      opacity: 1.00 !important;">
            <span class="share-button-counter ng-binding" style="color:#000000 !important;
                  background:#fafafa !important; // and this line
                  opacity: 1.00 !important;">0</span>
            <div class="share-button-sizing" ng-style="config.customColorsEnabled &amp;&amp; config.appName === 'floated_share_buttons' ? config.customColors : {}" style="color: rgb(0, 0, 0); opacity: 1; background: rgb(250, 250, 250);">
              <i class="shareaholic-service-icon service-facebook" ng-style="config.customColorsEnabled ? config.customColors : {}" style="color: rgb(0, 0, 0); opacity: 1; background: rgb(250, 250, 250);"></i>
              <span class="share-button-verb ng-binding" style="color:#000000 !important">
                <b class="ng-binding">Share</b>

              </span>
            </div>
          </div>

You are calling that function before DOM is fully loaded. 您正在DOM完全加载之前调用该函数。

So jQuery(".shareaholic-share-button-container") returns empty array. 因此jQuery(".shareaholic-share-button-container")返回空数组。 (because that element is not even made when you call this code) (因为调用此代码时甚至没有创建该元素)

  1. If .shareaholic-share-button-container is created from html file directly, then place your javascript at the bottom of the page. 如果.shareaholic-share-button-container是直接从html文件创建的,则将您的javascript放在页面底部。

  2. If .shareaholic-share-button-container is created dynamically, call javascript after fully rendered. 如果.shareaholic-share-button-container是动态创建的,请在完全渲染后调用javascript。
    For example in your case, you can use window ready event callback. 例如,您可以使用窗口就绪事件回调。

     jQuery(window).ready(function(){ jQuery(".shareaholic-share-button-container").css("background-color", "red");}); 

Edit: Ok, your problem is 2, but it is created asynchronously, so you don't know exact time when it created and even you cannot inject source code right after that. 编辑:好的,您的问题是2,但是它是异步创建的,因此您不知道创建它的确切时间,甚至之后也无法注入源代码。
One solution is observing every DOM element creation event. 一种解决方案是观察每个DOM元素创建事件。

jQuery('body').bind("DOMSubtreeModified", function(e) {
    var $tmp = jQuery(e.target).find(".shareaholic-share-button-container");
    if($tmp.length){
        $tmp.css("background-color", "rgba(0,0,0,0)");
    }
});

CAUTION This solution does not guarantee of working on cross browser environment. 注意此解决方案不能保证可以在跨浏览器环境中工作。 and may cause performance issue 并可能导致性能问题

you set a background color but it is absent, because you set opacity 0 (zero). 您设置了背景色,但背景色不存在,因为您将不透明度设置为0(零)。 Try eg 50% opacity 尝试例如50%的不透明度

jQuery(".shareaholic-share-button-container").css("background", "rgba(0,0,0,50)"); // 50% opacity

and then customize your opacity. 然后自定义您的不透明度 0 is zero opacity. 0是零不透明度。 Transparency is a value >0 and <100. 透明度是> 0和<100的值。 Choose the value of your transparency. 选择透明度的值。

if your need is 100% transparency, jquery provides 如果您的需求是100%透明,jQuery会提供

$(".shareaholic-share-button-container").css('background-color','transparent');

To override !important css check this link: How to override css containing '!important' using jquery? 要覆盖!important CSS,请检查以下链接: 如何使用jquery覆盖包含'!important'的CSS?

Try this: If rgba not working in jQuery then background-color property and opacity can be given by writing the code in two different way. 尝试以下操作:如果rgba在jQuery中不起作用,则可以通过以两种不同方式编写代码来提供background-color属性和opacity Also, !important does not work in jQuery, only use in css 此外, !important在jQuery中不起作用,仅在CSS中使用

<script type="text/javascript">
$(document).ready(function(){


$(".shareaholic-share-button-container").css("background-color", "#000");
$(".shareaholic-share-button-container").css("opacity", "0.5");

});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM