小编Bry*_*yan的帖子

带有条件逻辑的JQuery .css()函数

我正在某些JQuery函数中练习条件逻辑.$('h2').css({backgroundColor: 'red'});工作,但当我添加下面的条件逻辑时,它不再有效.如果我将返回值更改为字符串,它将消除错误,但它仍然不会更改背景颜色.我究竟做错了什么?

$('h2').css({function(){
    if (1 === 1){
        return backgroundColor: 'red';
    }
    else {
        return backgroundColor: 'purple';
    }
}});
Run Code Online (Sandbox Code Playgroud)

javascript css jquery function

3
推荐指数
3
解决办法
2605
查看次数

JavaScript - 如果语句总是返回true

我有一些JavaScript,我想在HTML中搜索一个类名,然后检测该div中几个元素的高度,将它们添加到一起,并在警报中显示总高度.以下代码似乎运行完美,但我注意到代码将运行,无论类名是什么,即使HTML中不存在该类.如何重写if语句,以便只有在遇到具有指定类名的div时才运行代码?我不希望它检测错误的h1和p元素的高度.谢谢你的帮助.

HTML:

<div class="testing">
    <h1>Understanding Scope</h1>
    <p>By understanding code's <em>scope</em>, we know when that code affects only one part of our code, or the entire codebase.  If we create things that are <em>global</em> in scope, we are giving any code the supreme power to control our code.   So we want to protect our code by being very careful when creating things that are global in scope. This is especially important if you plan on using JavaScript libraries like jQuery.</p>
</div> …
Run Code Online (Sandbox Code Playgroud)

javascript if-statement

0
推荐指数
1
解决办法
289
查看次数

标签 统计

javascript ×2

css ×1

function ×1

if-statement ×1

jquery ×1