繁体   English   中英

jQuery .css()不起作用

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

我正在尝试在我的网站上使用jQuery更改内联CSS。 到目前为止,我已经尝试了以下代码:

<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>

我也尝试过:

<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>

<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>

但是,没有任何效果。 有趣的是,当打开Cloudflare的开发模式时,代码可以工作,但是当关闭Cloudflare的开发模式时,代码不起作用(即使清除了Cloudflare缓存并禁用了缩小/火箭加载程序)。

感谢所有帮助!

编辑:此代码的全部重点是替换!important内联CSS并将背景更改为完全透明,即不透明度为零。 因此rgba(0,0,0,0)是正确的和预期的。

该代码的目的是删除!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>

您正在DOM完全加载之前调用该函数。

因此jQuery(".shareaholic-share-button-container")返回空数组。 (因为调用此代码时甚至没有创建该元素)

  1. 如果.shareaholic-share-button-container是直接从html文件创建的,则将您的javascript放在页面底部。

  2. 如果.shareaholic-share-button-container是动态创建的,请在完全渲染后调用javascript。
    例如,您可以使用窗口就绪事件回调。

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

编辑:好的,您的问题是2,但是它是异步创建的,因此您不知道创建它的确切时间,甚至之后也无法注入源代码。
一种解决方案是观察每个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)");
    }
});

注意此解决方案不能保证可以在跨浏览器环境中工作。 并可能导致性能问题

您设置了背景色,但背景色不存在,因为您将不透明度设置为0(零)。 尝试例如50%的不透明度

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

然后自定义您的不透明度 0是零不透明度。 透明度是> 0和<100的值。 选择透明度的值。

如果您的需求是100%透明,jQuery会提供

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

要覆盖!important CSS,请检查以下链接: 如何使用jquery覆盖包含'!important'的CSS?

尝试以下操作:如果rgba在jQuery中不起作用,则可以通过以两种不同方式编写代码来提供background-color属性和opacity 此外, !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