将JavaScript中的字符串与css内容中的字符串进行比较

时间:2017-06-18 14:46:24

标签: javascript jquery html css

我正在尝试基于css内容属性的字符串比较创建条件语句。

到目前为止,这是我的代码,但即使字符串相同,比较也会返回false。

CSS

.right-section::before {
        content:" the blue dog";
     /*   position: relative; */
        top: -50px; 
        }

的Javascript

jQuery(document).ready(function($) {
        var content = getComputedStyle(document.querySelector('.right-section'), ':before').getPropertyValue('content');
         var content2 = " the blue dog";
        if( content == content2) {
           console.log("CSS  running");
            $("#margin-adjust").css("margin-top", "10px !important");
          } 
            });

这是一个指向codepen的链接 https://codepen.io/anon/pen/EXZrwB?editors=1111

任何帮助表示赞赏!

2 个答案:

答案 0 :(得分:4)

CSS属性值包含双引号,因此您需要与具有它们的字符串值进行比较:

 var content2 = '" the blue dog"';

......然后他们就会平等。

答案 1 :(得分:1)

虽然字符串不一样......

content评估为" the blue dog" content2评估为the blue dog