简体   繁体   English

如何使用+ =运算符

[英]How to use += operator

Since I started using JQuery ive always wondering how does this operator work in JQuery Example: 自从我开始使用JQuery以来,我总是想知道这个运算符在JQuery示例中如何工作:

for(var i = 0;i<=4;i++)
{
document.getElementById("mydiv").innerText += i;//<- works as expected
}

//results will be 0,1,2,3,4

but if i use JQuery instead i dont know how to do it 但如果我使用JQuery而不知道该怎么做

for(var i = 0;i<=4;i++)
{
$("mydiv").text(+i)//<- NO!
$("mydiv").text+(i)//<- NO!
$("mydiv").+text(i)//<- JAJA COME ON!
$("mydiv").text(i)+//<- I guess that was stupid


}

This isn't possible like this. 这是不可能的。 Unlike innerText , text() is a method, not a property. innerText不同, text()是一个方法,而不是一个属性。

Try: 尝试:

$("mydiv").text($("mydiv").text() + i);

Or if you'd rather not make 2 references to $("mydiv") you can do: 或者,如果您不想对$("mydiv")进行2次引用,则可以执行以下操作:

$("mydiv").text(function(i,v){
   return v + i; 
});

You just need to work with jQuery here. 你只需要在这里使用jQuery。 Use the text method to extract the value, and then call it again to set the new value: 使用text方法提取值,然后再次调用它以设置新值:

for(var i = 0;i<=4;i++)
{
    var mydiv = $("mydiv"),
        t = mydiv.text();
    mydiv.text(t + i);
}

You can't use such shorcuts for jQuery methods, it only works for native assignment operators . 您不能将此类shorcuts用于jQuery方法,它仅适用于本机赋值运算符 For jQuery .text() , use a callback: 对于jQuery .text() ,使用回调:

$("#mydiv").text(function(index, oldtext) {
    return oldtext + i;
});

This callback thing works for all jQuery property "assignments", be it .html , .val , .prop , .attr , .css , or .text . 这个回调的东西适用于所有jQuery属性“赋值”,无论是.html.val.prop.attr.css还是.text

jQuery is not a programming language but a library built upon javascript, so, the rules are exactly the same as those you have to follow using javascript, and javascript has not been designed to understand these kinds of structures. jQuery不是一种编程语言,而是一个基于javascript的库,因此,规则与你必须使用javascript遵循的规则完全相同,而javascript并不是为了理解这些结构而设计的。


Edit 编辑

Of course I mean om(+1) does not increment op while o.p++ and o.p+=1 does : 当然我的意思是om(+1)不会增加opo.p++o.p+=1会:

var o = {};
o.p = 1;
o.m = function () { return this.p; };
o.m(+1); // o.p equals 1
o.p++;   // o.p equals 2
o.p+=1;  // o.p equals 3

Like other answers point out, jQuery is just a framework and is subject to same syntactic rules as any JavaScript code. 与其他答案一样,jQuery只是一个框架,并且遵循与任何JavaScript代码相同的语法规则。

While I see the advantage of passing in a function to .text() , I don't think it's a general purpose approach to solve your real problem : how to concatenate text when you use a function instead of a variable. 虽然我看到将function传递给.text()的优点,但我认为这不是解决实际问题的通用方法:如何在使用函数而不是变量时连接文本。

I'd favor the usage of Array.join() for efficient concatenation: 我赞成使用Array.join()进行有效的连接:

var textParts = [ $("mydiv").text() ];
for(var i = 0;i<=4;i++)
{
    textParts[textParts.length] = i;
}
$("mydiv").text(textParts.join(',')) // result: '...,1,2,3,4'

If you prefer the function approach over a loop, you could also use Array.map() . 如果您更喜欢循环的函数方法,也可以使用Array.map()

AFAIK DOM functions are rather slow so it's more effective to do the concatenation first and then set the div node value. AFAIK DOM功能相当慢,因此首先进行串联然后设置div节点值更有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM