簡體   English   中英

將 Onclick 事件轉換為頁面加載事件

[英]Convert Onclick Event to Page Load event

我在我的 wordpress 網站上使用Gtranslate plugin進行翻譯。 該插件生成了一個腳本,我將其附加在header.php中。 當我嘗試默認重新加載頁面時,它以英文顯示(未翻譯)。 當我點擊頂部的Finnish時,它切換到芬蘭語。 我希望我的網站只使用芬蘭語,所以有人幫助我將onclick事件轉換為pageload事件。

<!-- GTranslate: http://gtranslate.net/ -->
<div class="switcher notranslate">
  <div class="selected">
    <a href="#" onclick="return false;">
      <span class="gflag" style="background-position:-100px -100px;">
        <img src="/gtranslate/blank.png" height="16" width="16" alt="fi" />
      </span>Finnish
    </a>
  </div>
  <div class="option">
    <a href="#"  onclick="doGTranslate('fi|fi');jQuery(this).parent().parent().find('div.selected a').html(jQuery(this).html());return false;" title="Finnish" class="nturl selected">
      <span class="gflag" style="background-position:-100px -100px;">
        <img src="/gtranslate/blank.png" height="16" width="16" alt="fi" />
      </span>Finnish
    </a>
  </div>
</div>
<script type="text/javascript">
  jQuery('.switcher .selected').click(function() {
    if (!(jQuery('.switcher .option').is(':visible'))) {
      jQuery('.switcher .option')
      .stop(true, true)
      .delay(50)
      .slideDown(800);
    }
  });
  jQuery('body').not('.switcher .selected').mousedown(function() {
    if (jQuery('.switcher .option').is(':visible')) {
      jQuery('.switcher .option')
      .stop(true, true)
      .delay(300)
      .slideUp(800);
    }
  });
</script>
<div id="google_translate_element2"></div>
<script type="text/javascript">
  function googleTranslateElementInit2() {
    new google.translate.TranslateElement({
      pageLanguage: 'fi',
      autoDisplay: false
    }, 'google_translate_element2');
  }
</script>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit2"></script>


<script type="text/javascript">
  /* <![CDATA[ */
  function GTranslateFireEvent(element, event) {
    try {
      if (document.createEventObject) {
        var evt = document.createEventObject();
        element.fireEvent('on' + event, evt)
      } else {
        var evt = document.createEvent('HTMLEvents');
        evt.initEvent(event, true, true);
        element.dispatchEvent(evt)
      }
    } catch (e) {}
  }

  function doGTranslate(lang_pair) {
    if (lang_pair.value) lang_pair = lang_pair.value;
    if (lang_pair == '') return;
    var lang = lang_pair.split('|')[1];
    var teCombo;
    var sel = document.getElementsByTagName('select');
    for (var i = 0; i < sel.length; i++)
      if (sel[i].className == 'goog-te-combo') teCombo = sel[i];
    if (document.getElementById('google_translate_element2') == null ||
        document.getElementById('google_translate_element2').innerHTML.length == 0 || 
        teCombo.length == 0 || 
        teCombo.innerHTML.length == 0) {
      setTimeout(function() {
        doGTranslate(lang_pair)
      }, 500)
    } else {
      teCombo.value = lang;
      GTranslateFireEvent(teCombo, 'change');
      GTranslateFireEvent(teCombo, 'change')
    }
  }

  function GTranslateGetCurrentLang() {
    var keyValue = document.cookie.match('(^|;) ?googtrans=([^;]*)(;|$)');
    return keyValue ? keyValue[2].split('/')[2] : null;
  }
  if (GTranslateGetCurrentLang() != null) jQuery(document).ready(function() {
    jQuery('div.switcher div.selected a').html(jQuery('div.switcher div.option').find('span.gflag img[alt="' + GTranslateGetCurrentLang() + '"]').parent().parent().html());
  });
  /* ]]> */
</script>

為我工作!

 add_action('wp_footer', 'my_enqueue_front_scripts',0); function my_enqueue_front_scripts(){ echo '<script type="text/javascript"> doGTranslate("en|iw");return false; </script>'; }

根據我的理解,以下代碼正在將頁面翻譯成Finnish

doGTranslate('fi|fi');
jQuery(this)
  .parent()
  .parent()
  .find('div.selected a')
  .html(jQuery(this).html());
return false;

您只需在$(document).ready(function(){...})上調用此代碼

使用這個腳本:

<script>
    $(document).ready(function(){
        doGTranslate('fi|fi');jQuery(this).parent().parent().find('div.selected a').html(jQuery(this).html());return false;
    }
</script>

jusy 將它添加到您的標題(我認為如果您在標題中的所有腳本之后鍵入它,它會起作用。)

而不是在頁面加載時調用doGTranslate() 。編寫腳本以在加載時單擊<a>標記。

<a href="#" id="load_clicked" onclick="doGTranslate('fi|fi');jQuery(this).parent().parent().find('div.selected a').html(jQuery(this).html());return false;" title="Finnish" class="nturl selected"><span class="gflag" style="background-position:-100px -100px;"><img src="/gtranslate/blank.png" height="16" width="16" alt="fi" /></span>Finnish</a>

    <script type="text/javascript">
    $( window ).load(function() {
      $('#load_clicked').click();
    });
    </script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM