Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

daniele bianca

problema com jquery attr

Recommended Posts

estou com mais um projeto onde o cliente tem a opção de mudar o estilo do menu do painel.
ao abrir, a pagina devera verificar se existe alguma opção escolhida, se não tiver ela vai abrir css 1.
se já existir alguma opção de estilo escolhida abre o css correspondente.
exemplo:
se não tiver estilo escolhido abre o css 1.
ao escolher 'estilo classico' abre o css 2.
ao escolher 'estilo moderno' abre o css 3.
ao escolher 'estilo musical' abre o css 4.
ao escolher 'estilo tec' abre o css 5.
e assim por diante.
a escolha deverá ser memorizada por 100 dias.
consegui encontrar o jquery cookie:
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

 

e o pequeno script a troca:

if($.cookie("cookiename")) {
       $("link").attr("href",$.cookie("cookiename"));
};
$(document).ready(function() {
      $("#nav li a").click(function() { 
            $("link").attr("href",$(this).attr('rel'));
            $.cookie("cookiename",$(this).attr('rel'), {expires: 100, path: '/'});
            return false;

      });
});

 

 

o unico problema é: a pagina tem varios elementos de css diferentes (<link rel="stylesheet" type="text/css" href="estilo.css">), e quando esse código é acionado ele substitui todas as folhas de estilo.
existe alguma forma de ao invés de substituir, adicionar uma nova folha de estilo (como pelo elemento .html())?
muitooooooo obrigado gente.

Compartilhar este post


Link para o post
Compartilhar em outros sites
não entendi vinicius.
como isso seria aplicado ao meu script?
porque são folhas seguidas no html da página mesmo como:
<link rel="stylesheet" type="text/css" href="folhaestilo.css">
<link rel="stylesheet" type="text/css" href="folhaestilo.css">
<link rel="stylesheet" type="text/css" href="folhaestilo.css">
<link rel="stylesheet" type="text/css" href="folhaestilo.css">

<link rel="stylesheet" type="text/css" href="estilodomenuaqui.css">

 

entende?

 

acho que ainda vou precisar de ajuda, pois não entendi. muito obrigado.

Compartilhar este post


Link para o post
Compartilhar em outros sites

ao invés de acessar o elemento LINK no jquery, você pode acessar apenas o primeiro LINK

 

então no js

 

 


 

$(document).ready(function() { $("#nav li a").click(function() { $("link:first-child").attr("href",$(this).attr('rel')); $.cookie("cookiename",$(this).attr('rel'), {expires: 100, path: '/'}); return false; }); });

Compartilhar este post


Link para o post
Compartilhar em outros sites

oi vinicius,

 

consegui resolver. muitoooooooooooooooooooooooo obrigado.

 

só ficou uma dúvida. como eu faço pra que todas as páginas recuperem o mesmo cookie sem que o cliente tenha que selecionar o estilo pagina por pagina?

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.