简体   繁体   English

jQuery代码在IE9或更低版本中不起作用

[英]JQuery Code not working in IE9 or lower versions

EDIT* 编辑*

In one of the pages in our website there is a pirate with a speech bubble that tells jokes. 在我们网站的其中一个页面上,有一个海盗,上面有讲话泡泡,讲着笑话。 When the bubble is clicked the joke changes to another one at random in the list. 单击气泡后,笑话在列表中随机更改为另一个笑话。 For some reason this will not work in IE9 or lower. 由于某些原因,这在IE9或更低版本中不起作用。 Can anyone tell me why? 谁能告诉我为什么?

I have checked the console and this error appears 我已经检查过控制台,并且出现此错误

"CE2.w.external.InPrivateFilteringEnabled is not a function" 

Here is the script: 这是脚本:

$(document).ready(function () {
    var list = $(".joke-list li").toArray();
    var elemlength = list.length;

    // Encapsulate random value assignment within a function
    var setRandomValue = function(element) {
        var randomnum = Math.floor(Math.random() * elemlength);
        var randomitem = list[randomnum];
        $(element).html($(randomitem).text());
    }

    // Bind click button
    $('.speech').click(function () {
        setRandomValue(this);
    });

    // Set random value for the first time
    setRandomValue($('.speech'));
    $('.speech').css("width: 1000px;");
});

Here is a fiddle that I have thrown together to show the fault: http://jsfiddle.net/uRd6N/116/ 这是我为了说明问题而提出的一个小提琴: http : //jsfiddle.net/uRd6N/116/

try this, simpler & cleaner code 试试这个,更简单更干净的代码

$(document).ready(function () {

var list = $(".joke-list").find("li").map(function(){return $(this).text();});

 // Bind click 
$(document.body).on('click','.speech', function () {
  var rndnum=Math.floor(Math.random() * list.length);
  console.log(rndnum); 
   $(this).html(list[rndnum]);
});
//first rand joke 
$(".speech").trigger("click");

}); });

after seeing your html i guess your problem is that your speech div is parent of your joke list 看到您的html后,我想您的问题是您的语音div是您的笑话列表的父项

I suppose it's the missing semicolon in the function assigment. 我想这是功能分配中缺少的分号。 Try this: 尝试这个:

// Encapsulate random value assignment within a function
var setRandomValue = function(element) {
   [...]
}; // add semicolon here

You are missing a ; 你想念一个; at your function below. 在下面的功能中。

Try this: 尝试这个:

$(document).ready(function () {
var list = $(".joke-list li").toArray();
var elemlength = list.length;

// Encapsulate random value assignment within a function
var setRandomValue = function(element) {
    var randomnum = Math.floor(Math.random() * elemlength);
    var randomitem = list[randomnum];
    element.html($(randomitem).text());
};// <----';' is missing here ie will create issues here

// Bind click button
$('.speech').click(function () {
    setRandomValue($(this));
});

    // Set random value for the first time
    setRandomValue($('.speech'));
    $('.speech').css("width","1000px"); //<---apply css this way;
});

see if this solves your issue. 看看这是否可以解决您的问题。

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

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