首页 \ 问答 \ .get()无法按预期工作(.get() not working as expected)

.get()无法按预期工作(.get() not working as expected)

如果这是一个非常愚蠢的错误,我很抱歉,但我对.get()函数有点新意。 我完全按照本页所解释的那样使用它,但它没有按预期工作。 这是他们使用的代码

console.log( $( "li" ).get( 0 ) );

所以我试着做同样的事情:

$('.a').get(0).addClass('g');

从理论上讲,这应该将类'g'添加到具有类'a'的第一个元素,但它不会。 我已经手动将类'g'添加到第一个元素,它确实显示出来,所以它是jQuery不能正常工作。

jQuery 2.1.0


I apologize if this is a really dumb mistake, but I'm a little new to the .get() function. I used it exactly as was explained on this page, but it did not work as expected. This is the code they used

console.log( $( "li" ).get( 0 ) );

So I tried to do something to the same effect:

$('.a').get(0).addClass('g');

Theoretically, this should add the class 'g' to the first element with the class 'a', but it doesn't. I've already manually added the class 'g' to the first element and it does show up, so it's the jQuery that's not working.

jQuery 2.1.0


原文:https://stackoverflow.com/questions/28834870
更新时间:2023-08-02 20:08

最满意答案

|=是布尔逻辑运算符|的复合赋值运算符( JLS 15.26.2 ) ( JLS 15.22.2 ); 不要与条件或者||混淆 ( JLS 15.24 )。 还有&=^=对应于布尔逻辑&^的复合赋值版本。

换句话说,对于boolean b1, b2 ,这两个是等价的:

 b1 |= b2;
 b1 = b1 | b2;

逻辑运算符( &| )与其条件对应( &&|| )的区别在于前者不“shortcircuit”; 后者。 那是:

  • &| 总是评估两个操作数
  • &&|| 有条件地评估正确的操作数; 只有当其值可能影响二进制操作的结果时,才对该右操作数进行评估。 这意味着在以下情况下不评估正确的操作数:
    • &&的左操作数计算为false
      • (因为不管什么是正确的操作数评估,整个表达式都是false
    • ||的左操作数 评估为true
      • (因为无论正确的操作数如何评估,整个表达式都是true

所以回到你的原始问题,是的,该结构是有效的,而|=并不完全相同的快捷方式为=|| ,它会计算你想要的。 由于您的使用中的|=运算符的右侧是一个简单的整数比较操作,事实上| 不短路不重要。

有些情况下,当需要短路或甚至需要时,但是您的方案不是其中之一。

不幸的是,与其他一些语言不同,Java没有&&=||= 。 这在问题中讨论了为什么Java不具有条件和条件或运算符的复合分配版本? (&& =,|| =)


The |= is a compound assignment operator (JLS 15.26.2) for the boolean logical operator | (JLS 15.22.2); not to be confused with the conditional-or || (JLS 15.24). There are also &= and ^= corresponding to the compound assignment version of the boolean logical & and ^ respectively.

In other words, for boolean b1, b2, these two are equivalent:

 b1 |= b2;
 b1 = b1 | b2;

The difference between the logical operators (& and |) compared to their conditional counterparts (&& and ||) is that the former do not "shortcircuit"; the latter do. That is:

  • & and | always evaluate both operands
  • && and || evaluate the right operand conditionally; the right operand is evaluated only if its value could affect the result of the binary operation. That means that the right operand is NOT evaluated when:
    • The left operand of && evaluates to false
      • (because no matter what the right operand evaluates to, the entire expression is false)
    • The left operand of || evaluates to true
      • (because no matter what the right operand evaluates to, the entire expression is true)

So going back to your original question, yes, that construct is valid, and while |= is not exactly an equivalent shortcut for = and ||, it does compute what you want. Since the right hand side of the |= operator in your usage is a simple integer comparison operation, the fact that | does not shortcircuit is insignificant.

There are cases, when shortcircuiting is desired, or even required, but your scenario is not one of them.

It is unfortunate that unlike some other languages, Java does not have &&= and ||=. This was discussed in the question Why doesn't Java have compound assignment versions of the conditional-and and conditional-or operators? (&&=, ||=).

相关问答

更多
  • Java中分配的优先级最低。 因此,你的前两个表达式相当于: if ( b = (true || b==true) ); if((b == true || b)= true); 第二个不编译,因为表达式(b==true || b)不是一个lval(可以赋值的东西)。 如果您添加括号,您可以在OR之前完成作业,并且一切正常。 Assignments have the lowest precedence in Java. Thus, your first two expressions are equivale ...
  • 这个运营商 X <<= 4; 相当于 X = X << 4; 它遵循与其他复合赋值运算符相同的规则。 This operator X <<= 4; is equivalent to X = X << 4; It follows the same rules as other compound assignment operators.
  • 我认为你不会把任务和平等比较混淆。 我认为你的编译器只是给你一个令人困惑的错误信息。 这个程序: Integer i = 4; i ==null; 应该给出这样的错误: Program.java:8: not a statement i ==null; 你的第一个程序应该正确编译。 也许在文本中有一些不可见的unicode字符会让编译器感到困惑。 尝试删除整个功能并再次输入。 I don't think you are confusing assignment and ...
  • 不是。赋值总是Python中的一个语句。 这就是为什么在if语句中赋值的东西,在其他语言中是可以接受的,在Python中是禁止的。 No. An assignment is always a statement in Python. That's why things like assignment within if statements, which is acceptable in some other languages, is forbidden in Python.
  • |=是布尔逻辑运算符|的复合赋值运算符( JLS 15.26.2 ) ( JLS 15.22.2 ); 不要与条件或者||混淆 ( JLS 15.24 )。 还有&=和^=对应于布尔逻辑&和^的复合赋值版本。 换句话说,对于boolean b1, b2 ,这两个是等价的: b1 |= b2; b1 = b1 | b2; 逻辑运算符( &和| )与其条件对应( &&和|| )的区别在于前者不“shortcircuit”; 后者。 那是: &和| 总是评估两个操作数 &&和|| 有条件地评估正确的操作数; ...
  • 我自己的简写or分配给我,虽然它是非短路or代替逻辑or 。 因为它可以作为分配的简短版本和or:ing有时用于布尔,因为没有|| =。 但重要提示:在这种情况下,即使retValue可能已经为true它也将调用这两种方法 所以等价(逻辑明智)语句可以是几个,但有些语句可能是: boolean a = mActionBarHelper.onCreateOptionsMenu(menu); boolean b = super.onCreateOptionsMenu(menu); boolean retValu ...
  • 在C ++中,你可以编写例子 ( total_marks/=1000 ) *= 100; 但是在C中,这段代码不会编译。 在C(和C ++)中,您可以使用逗号运算符按以下方式编写代码 total_marks/=1000, total_marks *= 100; 我的意思是如果你在表达中需要这个。 In C++ you could write for example ( total_marks/=1000 ) *= 100; However in C this code will not compile ...
  • 这是不可能的。 这些语言的工作方式完全不同。 Java中的SomeClass x是SomeClass对象的某种指针或引用。 这是一个间接,所以你可以有多个引用同一个对象的原因。 C ++中的SomeClass x字面上是对象。 因此, SomeClass y实际上是一个完全不同的对象。 没有间接性。 所以没有办法让另一个参考。 C ++提供了指针( SomeClass* x )和引用( SomeClass& x )来处理需要间接的时间。 也许那些是你真正想要使用的,虽然它取决于你首先提出这个问题的原因。 回 ...
  • 请注意,在所有情况下,k的赋值都会覆盖右侧可能发生的任何增量。 在线发表评论: int k = 12; k += k++; System.out.println(k); // 24 在使用该值后, k++意味着增量,因此这与编码k = 12 + 12 k = 12; k += ++k; System.out.println(k); // 25 ++k表示在使用该值之前递增,因此这与编码k = 12 + 13 k = 12; k = k + k++ ...
  • Java中有许多运算符。 但“复合比较运算符”不是其中之一。 你应该从像“Head first Java”这样的好书中阅读Java基础知识。 要回答这个特定问题, b1 |= b2是复合赋值。 =将b1|b2的结果赋给LHS操作数,即b1 。 由于现在很清楚它是一个assignment运算符而不是比较,因此b1 |= b1的结果将与b1 = b1|b1 。 (注意|这里是两个数字之间的逻辑OR而不是|| ,它是一个条件运算符。 |和||有不同的含义) HTH。 There are many operator ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。