如果 id/class 在可调整大小的小部件选项中不存在,则设置另一个字符串 (jquery-ui)

Set another string if id/class does not exist in the options of resizable widget (jquery-ui)

我根据以下参考实现了一个自定义小部件(alsoResizeReverse): jQuery UI Resizable alsoResize reverse(我使用接受的答案)

但是,我想知道如果alsoResizeReverse 上指定的id 不存在,是否有任何解决方案可以设置另一个id?

例如,这是我当前的代码:

$("#select").resizable({
        handles: "s",
        alsoResizeReverse: "#content-timesheet",
        minHeight: 74.25,
        maxHeight: 378
    });

如果"#content-timesheet"不存在,我想设置另一个id。因此,或多或少,逻辑是这样的:

if($('#content-timesheet').length){
    alsoResizeReverse = "#content-timesheet";
}else{
    alsoResizeReverse = "#content-useditems";
}

提前致谢。

可以在三元表达式中检查jQuery对象的length属性。试试这个:

alsoResizeReverse: $('#content-timesheet').length ? '#content-timesheet' : '#content-useditems',

MDN 的三元引用:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator