我想在页面具有特定 cookie 时触发点击我的视频以显示。 但无论如何,它对我不起作用,我是 jQuery 的新手,非常感谢您的帮助。
这是我的代码(要查看完整的代码,请查看工作 JSFIDDLE
jQuery(document).ready(function($) {
jQuery('.video-thumb').click(function() {
var vidId = $(this).attr('id');
var vidBg = $(this).attr('data-bg');
$('#container').html('<iframe data-color="'+vidBg+'" id="player_'+vidId+'" width="420" height="315" src="http://www.youtube.com/embed/' + vidId + '?enablejsapi=1&autoplay=1&autohide=1&showinfo=0" frameborder="0" allowfullscreen></iframe>');
});
});
if (getCookie('username') && getCookie('temp')) {
$( ".initial" ).click();
aler('Cookies are set');
}
最佳答案
我不确定你想做什么,但在你的 fiddle 中,你只放置了 cookie 'username' 而你从未设置 cookie 'temp',所以这不会执行,因为 'temp' cookie 不存在:
if (getCookie('username') && getCookie('temp')) {
$( ".initial" ).click();
alert('Cookies are set');
}
如果您只是寻找您应该使用的 Cookie 之一
if (getCookie('username') || getCookie('temp')) {
$( ".initial" ).click();
alert('Cookies are set');
}
关于javascript - 检查现有 cookie 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27508941/