如何检查是否使用jQuery设置了cookie?

如何检查是否使用jQuery设置了cookie?,jquery,cookies,jquery-cookie,Jquery,Cookies,Jquery Cookie,我正在使用以下代码: $('#theme').attr('href', $.cookie("jquery-ui-theme")); 如果已经设置了cookie,则此操作有效。但是,如果cookie还没有被删除,我如何创建一个默认href呢?你可以使用一个基本上是一个if语句,只适合一行。它们的结构如下: 表情?valueIfTrue:valueIfFalse 我倾向于在这样的情况下使用它们,在这种情况下,如果未设置某些内容,则需要一个默认值 var href = $.cookie("jquer

我正在使用以下代码:

$('#theme').attr('href', $.cookie("jquery-ui-theme"));
如果已经设置了cookie,则此操作有效。但是,如果cookie还没有被删除,我如何创建一个默认href呢?你可以使用一个基本上是一个if语句,只适合一行。它们的结构如下:

表情?valueIfTrue:valueIfFalse

我倾向于在这样的情况下使用它们,在这种情况下,如果未设置某些内容,则需要一个默认值

var href = $.cookie("jquery-ui-theme") ? $.cookie("jquery-ui-theme") : 'http://www.example.com';
$('#theme').attr('href', href);
这相当于:

var href = $.cookie("jquery-ui-theme");
if (!href) {
    href = 'http://www.example.com';
}
$('#theme').attr('href', href);
您可以使用一个基本上是一个if语句,它只适合一行。它们的结构如下:

表情?valueIfTrue:valueIfFalse

我倾向于在这样的情况下使用它们,在这种情况下,如果未设置某些内容,则需要一个默认值

var href = $.cookie("jquery-ui-theme") ? $.cookie("jquery-ui-theme") : 'http://www.example.com';
$('#theme').attr('href', href);
这相当于:

var href = $.cookie("jquery-ui-theme");
if (!href) {
    href = 'http://www.example.com';
}
$('#theme').attr('href', href);

我不熟悉您的cookie插件,但只要使用三元运算符,并根据需要修改插件的代码即可:

$('#theme').attr('href', ($.cookie('jquery-ui-theme')!='') ? $.cookie('jquery-ui-theme') : 'your-default-value'))

另请参见:

我不熟悉您的cookie插件,但只要使用三元运算符,并在需要时修改插件的代码即可:

$('#theme').attr('href', ($.cookie('jquery-ui-theme')!='') ? $.cookie('jquery-ui-theme') : 'your-default-value'))
另请参见:

检查它是否存在:

if ($.cookie('jquery-ui-theme') != null) {
  $('#theme').attr('href', $.cookie("jquery-ui-theme"));
} else {
  $('#theme').attr('href', 'default');
}
检查它是否存在:

if ($.cookie('jquery-ui-theme') != null) {
  $('#theme').attr('href', $.cookie("jquery-ui-theme"));
} else {
  $('#theme').attr('href', 'default');
}

对于第一个值与条件相同的三元运算符,这里的建议很奇怪。将其缩短为:

$('#theme').attr('href', $.cookie('jquery-ui-theme') || "default");

你总是可以减少三元表达式A?A:B到更简单的A | | B

奇怪的是,这里对第一个值与条件相同的三元运算符提出了建议。将其缩短为:

$('#theme').attr('href', $.cookie('jquery-ui-theme') || "default");

你总是可以减少三元表达式A?A:B到更简单的A | | B

还请注意,这与jQuery cookie插件没有特别关系,而是默认值的一般解决方案。同样,请注意,这与jQuery cookie插件没有特别关系,而是默认值的一般解决方案