简单jquery脚本在safari中不起作用
这个脚本可以在firefox、IE和chrome中使用,但不能在safari中使用。有什么想法吗 真正奇怪的是,当我在简单jquery脚本在safari中不起作用,jquery,safari,Jquery,Safari,这个脚本可以在firefox、IE和chrome中使用,但不能在safari中使用。有什么想法吗 真正奇怪的是,当我在.get()回调的末尾放置alert()时,它会执行(即使在safari中也是如此)。所以代码没有崩溃,只是没有修改DOM <html> <head> <title>Title for this webpage</title> <script src="http://ajax.googleapis.com/aj
.get()
回调的末尾放置alert()
时,它会执行(即使在safari中也是如此)。所以代码没有崩溃,只是没有修改DOM
<html>
<head>
<title>Title for this webpage</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<!- CSS styling -------------------------------------------------------------->
<style type="text/css"/>
body{
margin:0px;
padding:0px;
font-family:verdana, arial, helvetica, sans-serif;
font-size:14px;
text-align:left;
width:900px;
margin-left:auto;
margin-right:auto;
position:relative;
background-color:#fff;
}
#mainBox{
width:700px;
margin:30px;
border:2px solid #ddd;
margin-left:auto;
margin-right:auto;
}
#mainHeader, #mainFooter, #mainContent{
margin: 5px;
padding:10px;
border: 2px dashed #ddd;
text-align:center;
}
.shuffleMe{
margin: 5px;
padding:10px;
border: 2px solid #ddd;
text-align:left;
}
</style>
<!- Jquery/javascript -------------------------------------------------------->
<script type="text/javascript">
//Code modified from: http://stackoverflow.com/questions/1134976/how-may-i-sort-a-list-alphabetically-using-jquery
$(document).ready(function(){
var my_list = $('.shuffleMe').get();
my_list.sort(function(){return( Math.random() >.5 );});
$.each(my_list, function(index, item) { $('#mainContent').append(item); });
});
</script>
</head>
<!- HTML body content -------------------------------------------------------->
<body>
<div id="mainBox">
<div id="mainHeader"> --- HEADER --- </div>
<div id="mainContent">
<div class="shuffleMe">AAA</div>
<div class="shuffleMe">BBB</div>
<div class="shuffleMe">CCC</div>
<div class="shuffleMe">DDD</div>
</div>
<div id="mainFooter"> --- FOOTER --- </div>
</div>
</body>
</html>
此网页的标题
身体{
边际:0px;
填充:0px;
字体系列:verdana、arial、helvetica、无衬线;
字体大小:14px;
文本对齐:左对齐;
宽度:900px;
左边距:自动;
右边距:自动;
位置:相对位置;
背景色:#fff;
}
#主机箱{
宽度:700px;
利润率:30像素;
边框:2px实心#ddd;
左边距:自动;
右边距:自动;
}
#主页眉,#主页脚,#主内容{
保证金:5px;
填充:10px;
边框:2个虚线#ddd;
文本对齐:居中;
}
shuffleMe先生{
保证金:5px;
填充:10px;
边框:2px实心#ddd;
文本对齐:左对齐;
}
//代码修改自:http://stackoverflow.com/questions/1134976/how-may-i-sort-a-list-alphabetically-using-jquery
$(文档).ready(函数(){
var my_list=$('.shuffleMe').get();
my_list.sort(函数(){return(Math.random()>.5);});
$.each(my_list,function(index,item){$('#mainContent')。append(item);});
});
---标题--
AAA
BBB
CCC
DDD
---页脚--
这是排序功能的问题。排序函数应返回-1(小于)、0(等于)或1(大于);它不应该返回布尔值。我认为,在这种情况下,它在其他浏览器中工作是一种侥幸
my_list.sort(function(){return( Math.random() - .5 );});
您需要更改排序功能。看这个。此代码正在运行:
var my_list = $('.shuffleMe').get();
my_list.sort(function(){
return 0.5 - Math.random();
});
$.each(my_list, function(index, item) {$('#mainContent').append(item); });
您是否尝试过使用另一个版本的jQuery来查看它是否是该版本中的错误?。另外,我在safari上看到了很多关于Math.random()的问题,可能是因为它修改了DOM,但是排序顺序没有改变。如果调整
$.each()
语句中每个项目的HTML,您将看到它正在更新。我正在试图弄清楚为什么sort()
方法不起作用。好建议。我刚刚尝试了几个不同版本的jquery——没有变化。此外,我还插入了以下行:alert(Math.random());每次重新加载都会给我一个不同的随机数字,所以我认为这不是问题所在。。。