有人能帮我找出错误吗?这个代码不起作用,我也不明白为什么。我想按价格分类我的单子。
<html>
<head>
</head>
<body>
<button type="button" onclick="LowToHigh()">Low To High</button>
<ul id="list1">
<li data-price="25" data-quality="8">Product1</li>
<li data-price="9" data-quality="9">Product2</li>
<li data-price="17" data-quality="6">Product3</li>
</ul>
<SCRIPT>
function LowToHigh()
{
$('#list1 li').sort(sort_li).appendTo('#list1');
function sort_li(a, b) {
var price1 = Number($(a).data('price'))
var price2 = Number($(b).data('price'))
if (price1 < price2) {return -1}
else {
if (price1 > price2) {return +1}
else {return 0}
}}
}
</SCRIPT>
</body>
</html>发布于 2018-10-03 16:34:20
尝试以这种方式将jquery添加到您的代码中,使用<script>标记,并看到您的排序从LowToHigh Order中运行良好。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
</head>
<body>
<button type="button" onclick="LowToHigh()">Low To High</button>
<ul id="list1">
<li data-price="25" data-quality="8">Product1</li>
<li data-price="9" data-quality="9">Product2</li>
<li data-price="17" data-quality="6">Product3</li>
</ul>
<script>
function LowToHigh()
{
$('#list1 li').sort(sort_li).appendTo('#list1');
function sort_li(a, b) {
var price1 = Number($(a).data('price'))
var price2 = Number($(b).data('price'))
if (price1 < price2) {return -1}
else {
if (price1 > price2) {return +1}
else {return 0}
}}
}
</script>
</body>
</html>
发布于 2018-10-03 16:38:42
您需要在您的jQuery块中包括<head></head>:
<script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>https://stackoverflow.com/questions/52631676
复制相似问题