2014-02-28 89 views
0

我見過類似於這個的其他一些跨瀏覽器問題,但它似乎歸結爲你使用jQuery的功能。這是我的兩段jQuery,因爲它們出現在我的HTML文檔中。爲了我的問題,我在兩個單獨的腳本標記中包含了它們。它是僅在Firefox中執行的第二個腳本標記。它不適用於Safari或Chrome,因此無法運行(甚至無法登錄控制檯)。爲什麼我的jQuery腳本只能在Firefox中工作?

<script type="text/javascript"> 
jQuery(document).ready(function() { 
      jQuery("#product_accordion_custom a").click(); 
      jQuery('#product_tabs_product_review_contents').appendTo('.product-collateral'); 

      //toothbrush changing - image to menu 
      var colorDropDown = jQuery("select#attribute92"); 
      var colorOptions = jQuery("select#attribute92 option"); 

       jQuery("div.slide a").click(function(e){ 
        var color = jQuery(this).find("img")[0].alt; 
        //colorDropDown.val(color).change(); 
        for(var i = 0;i<colorOptions.length;i++){ 
         s_color = colorOptions[i].innerText; 
         if(s_color==color){ 
          colorDropDown.val(colorOptions[i].value).change(); 
         } 
        } 

       }); 
     }); 
</script> 

<script type="text/javascript"> 
//toothbrush changing - menu to image 
jQuery(document).on("click", "select#attribute92 option:selected", function(){ 
    var selectorText = jQuery(this).text(); 
    var tag = jQuery("img[alt='"+selectorText+"']"); 
    jQuery("div.slide a").find(tag).click(); 
    console.log(selectorText); 
}); 
</script> 
+0

是的,上述內容在我的HTML文檔中顯示。 – sparecycle

回答

1

不同的瀏覽器發出的某些邊緣情況不同事件。當從下拉菜單中選擇一個選項時,Firefox顯然會觸發「點擊」事件;其他瀏覽器不會。

但是,無論瀏覽器如何,「更改」事件都應該觸發。以下是否適合你?

jQuery(document).on("change", "select#attribute92", function(){ 
    var selectorText = jQuery(this).children(":selected").text(); 
    var tag = jQuery("img[alt='"+selectorText+"']"); 
    jQuery("div.slide a").find(tag).click(); 
    console.log(selectorText); 
}); 

編輯:另外,作爲對方的回答表明,這把一個jQuery(文件)。就緒()函數中是很好的做法。

+0

先試試!非常感謝,傑森。 – sparecycle

1

你需要把這段代碼,

<script type="text/javascript"> 
//toothbrush changing - menu to image 
jQuery(document).on("click", "select#attribute92 option:selected", function(){ 
    var selectorText = jQuery(this).text(); 
    var tag = jQuery("img[alt='"+selectorText+"']"); 
    jQuery("div.slide a").find(tag).click(); 
    console.log(selectorText); 
}); 
</script> 

內,

jQuery(document).ready(function() { 


}); 

所以應該

<script type="text/javascript"> 
jQuery(document).ready(function() { 
    //toothbrush changing - menu to image 
    jQuery(document).on("click", "select#attribute92 option:selected", function(){ 
     var selectorText = jQuery(this).text(); 
     var tag = jQuery("img[alt='"+selectorText+"']"); 
     jQuery("div.slide a").find(tag).click(); 
     console.log(selectorText); 
    }); 
}); 
    </script> 
相關問題