【问题标题】:Some Css Property does not work with this Script某些 Css 属性不适用于此脚本
【发布时间】:2016-05-23 00:06:45
【问题描述】:

如果元素上的 CSS 属性不匹配,我想将用户重定向到另一个页面。

目前,这可行:

<div id="mydoom">
    myDoom
</div>

<style>
    #mydoom {
        position: relative;
        font-size: 25px;
        color: red;
        visibility: hidden;
        display:none;
    }
</style>

<script type='text/javascript'>
//<![CDATA[
$(document).ready(function() {
    $(function() {
        if (
            $("#mydoom").css('visibility') == 'hidden' && // See if the visibility is hidden.
            $("#mydoom").css('position') == 'relative' && // See if the position relative.
            $("#mydoom").css('display') == 'none' && // See if the display is set to none.
            $("#mydoom").css('font-size') == '25px' // See if the font-size is 25px.
       ) {
           //do nothing
       } else {
           window.location.replace("http://example.com");
       }
   });
})
//]]>
</script>

如果我将任何 CSS 属性更改为其他值,它会起作用并重定向页面,如果我将 position:relative 更改为 position:absolute,则页面重定向到示例。

但是,当我尝试检查 padding 属性时,它不会重定向。

例如:

<div id="mydoom">
    myDoom
</div>

<style>
    #mydoom {
        position: relative;
        padding: 12px;
        color: red;
        visibility: hidden;
        display:none;
    }
</style>

<script type='text/javascript'>
//<![CDATA[
    $(document).ready(function() {
        $(function() {
            if (
                $("#mydoom").css('visibility') == 'hidden' && // See if the visibility is hidden.
                $("#mydoom").css('position') == 'relative' && // See if the position is relative.
                $("#mydoom").css('display') == 'none' && // See if the display is set to none.
                $("#mydoom").css('padding') == '5px' // See if padding is 5px.
          ) {
              //do nothing
          } else {
             window.location.replace("http://example.com");
          }
     });
 })
//]]>
</script>

现在你看,我在 JavaScript 中设置了 padding 属性并将其设置为 5px,但在 CSS 中编写了 12px,现在页面应该被重定向但它不能。

为什么 padding 属性不起作用?

【问题讨论】:

  • @Steve 你能为我制作脚本吗,我不知道更多关于 javascript 的知识,如何使它成为可能,我检查了那个帖子,但没有发现任何工作......如何制作我现有的脚本使用填充...
  • 很遗憾,SO 不是脚本编写服务。查看链接问题的已接受答案,了解如何在 jQuery 中获取元素的填充。 (提示:您需要获取方向的填充,而不是 padding 速记)
  • @Steve 我也尝试了 padding-left 但它也不起作用..你能和我分享代码吗?谢谢

标签: javascript jquery html css


【解决方案1】:

我可能是错的,但很确定在 JQuery 中“填充”的简写并不总是被接受。更合适的方法是要求'padding-top','padding-right'......等等。 我会测试它并返回这里。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    相关资源
    最近更新 更多