我正在使用以下代码:
$('#theme').attr('href', $.cookie("jquery-ui-theme"));
如果 cookie 已设置,则此方法有效。但是如果 cookie 尚未被设置,我该如何制作默认的 href
最佳答案
奇怪的是,在这里看到关于三元运算符的建议,其中第一个值与条件相同。将其缩短为:
$('#theme').attr('href', $.cookie('jquery-ui-theme') || "default");
你总是可以减少三元表达式 A ? A : B
到更简单的 A || B
关于jquery - 如何检查 cookie 是否已使用 jQuery 设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9451065/