0

编辑*

在我们网站的一个页面中,有一个带有讲笑话的泡泡的海盗。单击气泡时,笑话会在列表中随机更改为另一个笑话。由于某种原因,这在 IE9 或更低版本中不起作用。谁能告诉我为什么?

我检查了控制台,出现了这个错误

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

这是脚本:

$(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;");
});

这是我拼凑起来以显示故障的小提琴:http: //jsfiddle.net/uRd6N/116/

4

3 回答 3

1

试试这个,更简单和更干净的代码

$(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");

});

看到你的 html 后,我想你的问题是你的语音 div 是你的笑话列表的父级

于 2013-01-30T12:42:06.517 回答
0

我想这是函数分配中缺少的分号。试试这个:

// Encapsulate random value assignment within a function
var setRandomValue = function(element) {
   [...]
}; // add semicolon here
于 2013-01-30T12:11:30.957 回答
0

您在下面的函数中缺少 a ;

试试这个:

$(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;
});

看看这是否能解决您的问题。

于 2013-01-30T12:24:48.300 回答