2014-02-28 93 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> 
相关问题