开发者

jQuery: Change text on click and show content

I have problem with my code.

I must have change text on click

<script type="text/javascript">
$(document).ready(function(){   
    $('.pokus1').hide().before('<a href="#" id="toggle-example1" class="show" ><div class="nadpis"><div class="nadpis2">Zumba</div><div class="open-close">Otevřít</div></div></a>');


        $('a#toggle-example1').click(function() {
        $('.pokus1').slideToggle(300);
        $(this).html($(this).html() == 
        '<a href="#" id="toggle-example1" class="show" ><div class="nadpis"><div class="nadpis2">Zumba</div><div class="open-close">Otevřít</div></div></a>' 
        ? 
        '<a href="#" id="toggle-example1" class="show" ><div class="nadpis"><div class="nadpis2">Zumba</div><div class="open-close">Otevřít</div></div></a>' 
        : 
        '<a href="#" id="toggle-example1" class="show" ><div class="nadpis"><div class="nadpis2">Zumba&开发者_运维问答lt;/div><div class="open-close">Zavřít</div></div></a>');
    return false;
    });
});

    </script>

But i Have 1. click text change, 2. click without effect, when I change

            $(this).html($(this).html() == 

to $(this).text($(this).text() ==

Scripst works good, but I must have HTML.

any advice ?

Thx for reply, Viktor


Try doing this instead:

$('.open-close', this).toggle(function(){
       var html = this.innerHTML;
       if(html != 'Otevřít'){
             this.innerHTML = 'Zavřít';
       }
})

Updated fiddle to change for open and close:

$('a#toggle-example1').click(function() {
    $('.pokus1').slideToggle(300);
    $('.open-close', this).toggle(function() {
        var html = this.innerHTML;
        if (html == 'Zavřít') {
            this.innerHTML = 'Otevřít';
        }
        else if (html == 'Otevřít') {
            this.innerHTML = 'Zavřít';
        }
        this.style.display = 'block';
    })
    return false;
});

Fiddle: http://jsfiddle.net/maniator/uUrRf/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜