简体   繁体   English

jQuery脚本在Webkit浏览器上不起作用

[英]jQuery script not working on webkit browsers

i've got this code working on mozilla, but ain't working on webkit browsers. 我已经在mozilla上运行了这段代码,但是在webkit浏览器上却无法运行

it's simple script, click on an 这是简单的脚本,请点击 with an assigned value, and shows an image. 具有指定的值,并显示图像。 then the other 's, get disabled attr. 然后其他的,被禁用attr。

can anyone help me to undertood why it's not working in webkit. 谁能帮助我了解为什么它不能在Webkit中工作。

info: it's for a FB app, working on shortstack 信息:适用于FB应用程序,可在短堆栈上使用

Thanks 谢谢

THE CODE: 编码:

<script type="text/javascript">
$(document).ready(function() {

$('#image,#image2,#image3,#image4,#image5').hide();

    $('option').click(function(e) {
    switch ($(this).attr('value')) {

      case 'ATENTA': 
        $("#image").show().click(function(){
            $(this).hide();
        });
        e.preventDefault();
        break;

      case 'CREATIVA':
        $("#image2").show().click(function(){
            $(this).hide();
        });    
        e.preventDefault();
        break;  

      case 'COQUETA':
        $("#image4").show().click(function(){
            $(this).hide();
        });        
        e.preventDefault();
        break;

      case 'PEGOTE': 
        $("#image3").show().click(function(){
            $(this).hide();
        });     
        e.preventDefault();
        break;

      case 'COLGADA':
        $("#image5").show().click(function(){
            $(this).hide();
        });    
        e.preventDefault();
        break;  
    }
    });

    $('select').change(function(){
    var value = $(this).val();
    $(this).children('option').each(function (){
      if ($(this).val() === value) {
          $(this).siblings('option').attr('disabled', true);
      }
        });
    });

});

I'm guessing that what's not working is the actual load of images. 我猜测无法正常工作的是图像的实际负载。 I'd think this is because of this bit of code: 我认为这是因为这段代码:

$('option').click(function(e) {

Browsers handle form elements differently. 浏览器对表单元素的处理方式不同。 Clicking on an option from a select might trigger the 'click' event for firefox, but not on Chrome, for example. 单击选择中的选项可能会触发Firefox的“单击”事件,但不会触发Chrome。

Try something like this: 尝试这样的事情:

$(document).ready(function() {

    $('#image,#image2,#image3,#image4,#image5').hide();


    $('select').change(function() {
        var value = $(this).val();

        $(this).children('option').each(function (){
            if ($(this).val() === value) {
                $(this).siblings('option').attr('disabled', true);
            }
        });

        switchImage(value);
    });

    var switchImage = function(toLoad) {
        switch (toLoad) {

          case 'ATENTA':
            $("#image").show().click(function(){
                $(this).hide();
            });
            break;

          case 'CREATIVA':
            $("#image2").show().click(function(){
                $(this).hide();
            });
            break;

          case 'COQUETA':
            $("#image4").show().click(function(){
                $(this).hide();
            });
            break;

          case 'PEGOTE':
            $("#image3").show().click(function(){
                $(this).hide();
            });
            break;

          case 'COLGADA':
            $("#image5").show().click(function(){
                $(this).hide();
            });
            break;
        }
        });
    };

});

What this is doing is using the 'change' event on the select tag itself to call the switchImage function, rather than depending on the 'click' event on the option tag. 这是在使用select标签本身的'change'事件来调用switchImage函数,而不是依赖option标签上的'click'事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM