2010-06-20 67 views
0

在Joomla中,我正在切换不使用国家/地区标志作为指示器的语言,而是使用jQuery的replaceWith()方法将文本从英语切换到南非荷兰语。使用jQuery更改文本后链接不可点击

问题我遇到的情况是,首先单击英语单词,将其更改为南非荷兰语,该链接不起作用。然而,当它需要返回时它确实在切换。

请欣赏帮助。这是我的我怎么想它应该工作的逻辑:

jQuery(document).ready(function($) { 
$(".item48").toggle(
    function() { 
    $(this).replaceWith("<li class='active item48' id='current'><a href='index.php?lang=af'><span>English</span></a></li>"); 
    }, 
    function() { 
    $(this).replaceWith("<li class='active item48' id='current'><a href='index.php?lang=en'><span>Afrikaans</span></a></li>"); 
    } 
); 

回答

1

你更换具有click处理器(.toggle()click处理程序,而你现在失去它)的元素,但你真的只需要更改链接,使用.html()这样的:

jQuery(document).ready(function($) { 
    $(".item48").toggle(
    function() { 
     $(this).html("<a href='index.php?lang=af'><span>English</span></a>"); 
    }, 
    function() { 
     $(this).html("<a href='index.php?lang=en'><span>Afrikaans</span></a>"); 
    } 
); 
}); 
1

这是因为事件处理程序不保持不变。您可以使用live作为替代方法,但更好的方法是更改​​href和文本。

jQuery(document).ready(function($) { 
$(".item48").toggle(
    function() { 
    $('a', this).attr('href', 'index.php?lang=en').find('span').text('English') 
    }, 
    function() {   
    $('a', this).attr('href', 'index.php?lang=af').find('span').text('Afrikaans') 
    } 
); 

或者使用$(this).html('HTML HERE')代替。