嗨,这是我的 js 代码。
var a = '<span> and have </span>' + $('#module_product_review_star_1 .pdp-link')[1] ? $('#module_product_review_star_1 .pdp-link')[1].outerHTML : 'not have';
alert(a);
我正在使用三元运算符来检查项目是否存在于 dom 中。但是,当该项目不在 dom 中时,此代码将失败。
这有效,
if($('#module_product_review_star_1 .pdp-link')[1]){
questiones = '<span> and have </span>' + $('#module_product_review_star_1 .pdp-link')[1].outerHTML;
}
但是我需要使用三元运算符并在一行中完成。有没有办法做到这一点?