因此,我正在为我正在创建的网站使用 Google 网站翻译器 ( http://translate.google.com/ )。
当用户单击西班牙语翻译按钮时,我想切换到不同的表单,但我无法触发更改,因为我基于更改的 html 类 (translated-ltr) 是动态创建的.
这似乎很容易......但我很困惑。这可以做到吗?请注意,我刚刚开始学习如何使用 javascript/jquery。
谷歌翻译按钮的代码:
<!-- Spanish Button -->
<a class="translation-links" href="#" class="spanish" data-lang="Spanish" onClick="" onMouseOver="MM_swapImage('spanish.btn','','images/ss_spanish_tout_on.jpg',1)" onMouseOut="MM_swapImgRestore()"><img src="images/ss_spanish_tout.jpg" style="margin-top:10px;" alt="Translate to Spanish!" id="spanish.btn" name="spanish.btn" /></a>
<!-- Code provided by Google -->
<div id="google_translate_element" style="display:none;"></div>
<script type="text/javascript">
function googleTranslateElementInit() {
new google.translate.TranslateElement({pageLanguage: 'en', includedLanguages: 'en,es', gaTrack: true, gaId: 'UA-10765676-1', autoDisplay: false}, 'google_translate_element'); //remove the layout
}
</script>
<script src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit" type="text/javascript"></script>
<script type="text/javascript">
function triggerHtmlEvent(element, eventName) {
var event;
if(document.createEvent) {
event = document.createEvent('HTMLEvents');
event.initEvent(eventName, true, true);
element.dispatchEvent(event);
} else {
event = document.createEventObject();
event.eventType = eventName;
element.fireEvent('on' + event.eventType, event);
}
}
<!-- Button click handler -->
$('.translation-links').click(function(e) {
e.preventDefault();
var lang = $(this).data('lang');
$('#google_translate_element select option').each(function(){
if($(this).text().indexOf(lang) > -1) {
$(this).parent().val($(this).val());
var container = document.getElementById('google_translate_element');
var select = container.getElementsByTagName('select')[0];
triggerHtmlEvent(select, 'change');
}
});
});
</script>
触发表单动作更改的代码:
if ($('html').hasClass('translated-ltr')) {
$('#contactForm').attr('action', 'kcontactl2_spanish.php');
}
原始表格链接:
<form method="post" action="kcontactl2.php" onsubmit="return validateForm();" name="contactSIE" id="contactForm">