简体   繁体   English

jQuery修改DOM中元素的内联CSS

[英]jQuery to modify inline CSS of element inside DOM

EDIT: I have already tried removeAttr, addClass, removeClass, changing the opacity... Basically the usual jQuery scripts to change inline CSS DO NOT WORK here! 编辑:我已经尝试过removeAttr,addClass,removeClass,更改不透明度...基本上,用于更改内联CSS的常用jQuery脚本在这里不起作用!

Website is at http://thehungrygeek.com 网站位于http://thehungrygeek.com

在此处输入图片说明

Unfortunately the background there is set by an !important inline CSS element that is located inside the DOM. 不幸的是,那里的背景是由位于DOM内部的!important内联CSS元素设置的。

Inline CSS element code is as follows: 内联CSS元素代码如下:

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

This code can't be seen with 'view source', but you will see it if you inspect the share buttons as per the image above. 无法使用“查看源代码”查看此代码,但是如果您按照上图检查共享按钮,则会看到它。 The element is inside the DOM. 元素在DOM内部。

The element is probably called by this line in the HTML code: 该元素可能在HTML代码中由以下行调用:

<script type='text/javascript' src='//dsms0mj1bbhn4.cloudfront.net/assets/pub/shareaholic.js' data-shr-siteid='82ff342d6c171e823bc5c49c19bf1b59' data-cfasync='false' async='async'>
</script>

Is there a way to use jQuery to change the inline CSS of an element within the DOM? 有没有一种方法可以使用jQuery更改DOM中元素的内联CSS?

So far the usual jQuery .css() do not work, I have tried: 到目前为止,普通的jQuery .css()无法正常工作,我已经尝试过:

<script type="text/javascript">
     jQuery(window).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>

I have also tried removeAttr, addClass, removeClass, changing the opacity... 我也尝试过removeAttr,addClass,removeClass,更改不透明度...

And a lot of other similar iterations of the usual code. 以及普通代码的许多其他类似迭代。 I have also tried the suggestions found at How to override css containing '!important' using jquery? 我还尝试了如何使用jquery覆盖包含'!important'的CSS的建议

Basically the usual jQuery scripts to change inline CSS DO NOT WORK here! 基本上,用于更改内联CSS的常用jQuery脚本在这里不起作用!

The problem seems to be using jQuery to target an element inside the DOM. 问题似乎是使用jQuery定位DOM中的元素。 Is there a way to do this? 有没有办法做到这一点?

Thanks in advance :) 提前致谢 :)

Since you are using jQuery, you can first use the removeAttr() method to remove the style attribute then apply the CSS. 由于使用的是jQuery,因此您可以先使用removeAttr()方法删除style属性,然后再应用CSS。

jQuery(window).ready(function() {
    jQuery(".shareaholic-share-button-container,.share-button-counter").removeAttr('style').css("background", "green");
});

WORKING DEMO 工作演示

This is just a demo , you can make changes as required. 这只是一个演示,您可以根据需要进行更改。 Also prefer to use addClass & removeClass 还喜欢使用addClass和removeClass

You can just set the opacity to 0 for the background to be transparent. 您可以将opacity设置为0以使背景透明。

jQuery(window).ready(function() {
    jQuery(".shareaholic-share-button-container,.share-button-counter").css("opacity", "0");
});

If css classes are not playing with padding and margin then you can try this tick- 如果CSS类未使用填充和边距,则可以尝试以下打勾:

jQuery(window).ready(function() {
    jQuery(".shareaholic-share-button-container").html("<div style='background: black'>"+ jQuery(".shareaholic-share-button-container").html() +"</div>");
    jQuery(".share-button-counter").html("<div style='background: black'>"+ jQuery(".share-button-counter").html() +"</div>");
});

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

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