jquery/Javascript语法

Riv*_*ver 4 javascript jquery popover twitter-bootstrap

我想弄清楚下面的语法是做什么的.这是来自Bootstraps popover的代码.

//title is a string
$tip.find('.popover-title')[ $.type(title) == 'object' ? 'append' : 'html' ](title)
                           ^                                                 ^
                       What does this symbol mean?                    Whats happening here?
Run Code Online (Sandbox Code Playgroud)

Ble*_*der 6

我们分手吧:

$tip.find('.popover-title')[ $.type(title) == 'object' ? 'append' : 'html' ](title)
Run Code Online (Sandbox Code Playgroud)

它可以分解为:

var found = $tip.find('.popover-title');
found[$.type(title) == 'object' ? 'append' : 'html'](title);
Run Code Online (Sandbox Code Playgroud)

和更多:

var found = $tip.find('.popover-title');

if ($.type(title) == 'object') {
  found['append'](title);
} else {
  found['html'](title);
}
Run Code Online (Sandbox Code Playgroud)

再来一点:

var found = $tip.find('.popover-title');

if ($.type(title) == 'object') {
  found.append(title);
} else {
  found.html(title);
}
Run Code Online (Sandbox Code Playgroud)


Nea*_*eal 5

它基本上是这样说的:

if(typeof title == 'object') {
    $tip.find('.popover-title').append(title);
}
else {
    $tip.find('.popover-title').html(title);
}
Run Code Online (Sandbox Code Playgroud)

[...]符号允许你动态地选择从功能$tip.find('.popover-title')对象.