javascript - 这个 jQuery 在做什么?

标签 javascript jquery jquery-plugins

我会称自己为中级 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/

相关文章:

javascript - 在轮播上滑动图像时出现奇怪的行为

javascript - 单击SVG元素时,如何暂停/停止音频播放?

javascript - 如何解决 SignalR 的类型错误

javascript - Revolution Slider - 仅在屏幕上时加载 slider

javascript - 将 jquery 选择器传递给插件内的子函数

javascript - 实现 jquery UI 自动完成以在您键入 "@"时显示建议

javascript - 为什么 jQuery 颜色选择器插件在 Firefox 中不起作用?

javascript - Facebook Javascript SDK - 是否有一次回调函数 fb :likes have been loaded?

javascript - 通过 asp.net 访问记事本、计算器

javascript - 如何在 mozilla 浏览器的 Select around arrow 中删除边框和箭头