Javascript 和 jquery 语法

标签 javascript jquery syntax

我不知道如何搜索这个,所以我在这里问。

我想弄清楚这段代码是如何工作的:

if ($("#main .oc .dc .image").removeAttr("style"), 1120 > wW && wW > 920)
    $("#main .oc .dc .image").css({
        width: Math.round(.3 * wW) + "px",
        left: "calc(50% - " + Math.round(.3 * wW / 4) + "px)"
    });

我的问题是 -

.removeAttr("style")1120 > wW 之间的逗号在这个语句的条件下有什么作用?

以及 Action 是如何执行的,-

$("#main .oc .dc .image").css(

在数字括号之前和 css 属性上缺少引号 -

{width:Math.round(.3*wW)+"px",left:"calc(50% - "+Math.round(.3*wW/4)+"px)"}

让我很困惑。

最佳答案

谢谢你的提问,m aksem...
感谢您的解释,@vlaz ...

我学到了一些新东西!
我做了一个测试片段来完成这个:

var wW = 1000;

$("#test1").on("click",function(){
    if ($("#main .oc .dc .image").removeAttr("style"), 1120 > wW && wW > 920)
        $("#main .oc .dc .image").css({
            width: Math.round(.3 * wW) + "px",
            left: "calc(50% - " + Math.round(.3 * wW / 4) + "px)"
        });
});

$("#test2").on("click",function(){
    if (1120 > wW && wW > 920)
        $("#main .oc .dc .image").css({
            width: Math.round(.3 * wW) + "px",
            left: "calc(50% - " + Math.round(.3 * wW / 4) + "px)"
        });
});

$("#test3").on("click",function(){
    if (false, 1120 > wW && wW > 920)
        $("#main .oc .dc .image").css({
            width: Math.round(.3 * wW) + "px",
            left: "calc(50% - " + Math.round(.3 * wW / 4) + "px)"
        });
});
.image{
    border:1px solid black;
    width:50%;
    position:absolute;
    left:20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="main">
    main
    <div class="oc">
        oc
        <div class="dc">
            dc
            <div class="image" style="color:red;">
                image
            </div>  
        </div>  
    </div>  
</div>
<br>
<br>
<input type="button" id="test1" value="Test 1 - Execute full condition"><br>
<input type="button" id="test2" value="Test 2 - Execute condition without the left hand part"><br>
<input type="button" id="test3" value="Test 3 - Execute condition with a FALSE in the left hand part"><br>

所以问题是条件中有两个参数,以逗号分隔。
可能还有更多。

在“测试 1”中,左边的条件被执行(红色,在内联样式属性中定义被移除),但是 bool 结果(应该是真的)在 if 语句。

只有最后一个条件会是。
“测试 3”证明了这一点,因为左手条件是 false

请注意,我将 wW 定义为 1000,以便使右侧条件评估为 true(因此,请查看效果)。
我还为 .image 定义了一个 CSS position:absolute

我同意@Jason P 的观点,即这个“技巧”大大降低了可读性。
我没有看到任何有用的案例。尽管如此,知道它还是一件好事!

关于Javascript 和 jquery 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40633804/

相关文章:

javascript - 以前在 Angular 矩js

javascript - 将函数应用于变量? JavaScript/jQuery

javascript - Javascript 中的 RGB 透明颜色值

javascript - 除了打开的元素外,在任何地方都进行点击时如何关闭元素?

javascript - 无法弹出 jQuery 对话框

SQL语法错误?

if-statement - = google 文档中的 IF 语法错误

javascript - 当用户单击提交按钮时,我想将对象类型转换为 j-son 以在 rest api 中发布数据

javascript - 使用 styled-component 在父组件的悬停状态下隐藏子组件

javascript - Dojo 树 - 延迟直到服务器返回数据