我会称自己为中级 jQuery 开发人员,但我对这条线(来自 Twitter 的 Bootstrap)在做什么感到困惑:
$tip.find('.help-popover-title')[ $.type(title) == 'object' ? 'append' : 'html' ](title)
具体来说,方括号之间的部分。谁能给我解释一下?
最佳答案
$tip // tip object
.find('.help-popover-title') // find elements of this class
// if the data inside the title variable is an object
// use the append method otherwise use html method
[$.type(title) == 'object' ? 'append': 'html']
(title) // lastly, execute the selected function and pass in the title var
内部语句使用三元运算符。它基本上是一行 if..else
语句
x = 5;
x === 5 ? true : false; // true
x === 4 ? true: false; // false
由于选择的方法在括号内,所以可以使用字符串来选择方法 它等同于:
$tip['append'](title) === $tip.append(title)
关于javascript - 这个 jQuery 在做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9169783/