2 qq 33599520 qq_33599520 于 2016.09.21 20:11 提问

还差两行代码,谁帮我完善下

<!DOCTYPE html>



第五章
<br> function Phone(desc,price,sale,pic){<br> this.desc=desc;<br> this.price=price;<br> this.sale=sale;<br> this.pic=pic;<br> }<br> var Phones=new Array();<br> Phones[0]=new Phone(&quot;移动GSM智能手机&quot;,2100.00,0,&quot;p1.jpg&quot;);<br> Phones[1]=new Phone(&quot;电信4G手机&quot;,1900.00,1,&quot;p2.jpg&quot;);<br> Phones[2]=new Phone(&quot;全网通手机 钛金属机身&quot;,4999.00,2,&quot;p3.jpg&quot;);<br> Phones[3]=new Phone(&quot;电信4G手机 黑色金边&quot;,2200.00,1,&quot;p4.jpg&quot;);<br> Phones[4]=new Phone(&quot;移动GSM S系列手机&quot;,1680.00,5,&quot;p5.jpg&quot;);<br> Phones[5]=new Phone(&quot;联通 经典高端商务手机&quot;,2080.00,10,&quot;p6.jpg&quot;);<br> Phones[6]=new Phone(&quot;联通4G W系列手机&quot;,1780.00,13,&quot;p7.jpg&quot;);<br> Phones[7]=new Phone(&quot;联通4G 经典手机&quot;,1280.00,27,&quot;p8.jpg&quot;);<br> function showPhones(){<br> var divPro=document.getElementById(&quot;show&quot;);<br> divPro.innerHTML=&quot;&quot;;<br> for(var i in Phones){<br> var dlObj=document.createElement(&quot;dl&quot;);<br> var content=&quot;<dt><img src='"+Phones[i]["pic"]+"'></dt>&quot;;<br> content+=&quot;<dd><p>$<span>&quot;+Phones[i][&quot;price&quot;]+&quot;</span></p>销量:<span>&quot;+Phones[i][&quot;sale&quot;]+&quot;</span></dd>&quot;;<br> content+=&quot;<dd>&quot;+Phones[i][&quot;desc&quot;]+&quot;</dd>&quot;;<br> dlObj.innerHTML=content;<br> dlObj.className=&quot;eachPro&quot;;<br> divPro.appendChild(dlObj);<br> }<br> }<br> function sortByPrice(){<br> for(var i=0;i<Phones.length;i++){<br> for(var j=0;j<Phones.length-1-i;j++){<br> if(Phones[j]["price"]>Phones[j+1][&quot;price&quot;]){<br> var temp=Phones[j];<br> Phones[j]=Phones[j+1];<br> Phones[j+1]=temp;<br> }<br> }<br> }<br> }<br> function sortBySale(){<br> for(var i=0;i<Phones.length;i++){<br> for(var j=0;j<Phones.length-1-i;j++){<br> if(Phones[j]["sale"]>Phones[j+1][&quot;sale&quot;]){<br> var temp=Phones[j];<br> Phones[j]=Phones[j+1];<br> Phones[j+1]=temp;<br> }<br> }<br> }<br> }<br> function sortPro(){<br> var selValue=document.getElementById(&quot;sort&quot;).value;<br> if(selValue==1){<br> //按照价格排序数组</p> <pre><code> } else if(selValue==2){ //按照销量排序数组 } //显示数组中保存的产品对象 } &lt;/script&gt; &lt;/head&gt; &lt;body onload=&quot;showPhones()&quot; onchange=&quot;sortPro()&quot;&gt; &lt;div id=&quot;product&quot;&gt; &lt;p class=&quot;kind&quot;&gt; 排序样式: &lt;select id=&quot;sort&quot;&gt; &lt;option value=&quot;0&quot;&gt;--请选择--&lt;/option&gt; &lt;option value=&quot;1&quot;&gt;价格&lt;/option&gt; &lt;option value=&quot;2&quot;&gt;销量&lt;/option&gt; &lt;/select&gt; &lt;/p&gt; &lt;div id=&quot;show&quot;&gt;&lt;/div&gt; &lt;/div&gt; &lt;/body&gt; </code></pre> <p></html></p>

2个回答

showbo
showbo   Ds   Rxr 2016.09.22 16:17
已采纳
 <!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>第五章</title>
    <script type="text/javascript">
        function Phone(desc, price, sale, pic) {
            this.desc = desc;
            this.price = price;
            this.sale = sale;
            this.pic = pic;
        }
        var Phones = new Array();
        Phones[0] = new Phone("移动GSM智能手机", 2100.00, 0, "p1.jpg");
        Phones[1] = new Phone("电信4G手机", 1900.00, 1, "p2.jpg");
        Phones[2] = new Phone("全网通手机 钛金属机身", 4999.00, 2, "p3.jpg");
        Phones[3] = new Phone("电信4G手机 黑色金边", 2200.00, 1, "p4.jpg");
        Phones[4] = new Phone("移动GSM S系列手机", 1680.00, 5, "p5.jpg");
        Phones[5] = new Phone("联通 经典高端商务手机", 2080.00, 10, "p6.jpg");
        Phones[6] = new Phone("联通4G W系列手机", 1780.00, 13, "p7.jpg");
        Phones[7] = new Phone("联通4G 经典手机", 1280.00, 27, "p8.jpg");
        function showPhones() {
            var divPro = document.getElementById("show");
            divPro.innerHTML = "";
            for (var i in Phones) {
                var dlObj = document.createElement("dl");
                var content = "<dt><img src='" + Phones[i]["pic"] + "'></dt>";
                content += "<dd><p>$<span>" + Phones[i]["price"] + "</span></p>销量:<span>" + Phones[i]["sale"] + "</span></dd>";
                content += "<dd>" + Phones[i]["desc"] + "</dd>";
                dlObj.innerHTML = content;
                dlObj.className = "eachPro";
                divPro.appendChild(dlObj);
            }
        }
        function sortPro() {
            var selValue = document.getElementById("sort").value;

            if (selValue == '1' || selValue == '2') {
                Phones.sort(function (a, b) { return a[selValue == '1' ? 'price' : 'sale'] - b[selValue == '1' ? 'price' : 'sale'] });
                showPhones();
            }
        }
    </script>
</head>
<body onload="showPhones()">
    <div id="product">
        <p class="kind">
            排序样式:
            <select id="sort" onchange="sortPro()"><!---事件加错地方了---->
                <option value="0">--请选择--</option>
                <option value="1">价格</option>
                <option value="2">销量</option>
            </select>
        </p>
        <div id="show"></div>
    </div>
</body>
</html>

showbo
showbo 降序的话将参数a,b的位置对调下就行
大约一年之前 回复
qq_33599520
qq_33599520   2016.09.21 20:12
  1. <!DOCTYPE html>
  2. 第五章
  3. function Phone(desc,price,sale,pic){
  4. this.desc=desc;
  5. this.price=price;
  6. this.sale=sale;
  7. this.pic=pic;
  8. }
  9. var Phones=new Array();
  10. Phones[0]=new Phone("移动GSM智能手机",2100.00,0,"p1.jpg");
  11. Phones[1]=new Phone("电信4G手机",1900.00,1,"p2.jpg");
  12. Phones[2]=new Phone("全网通手机 钛金属机身",4999.00,2,"p3.jpg");
  13. Phones[3]=new Phone("电信4G手机 黑色金边",2200.00,1,"p4.jpg");
  14. Phones[4]=new Phone("移动GSM S系列手机",1680.00,5,"p5.jpg");
  15. Phones[5]=new Phone("联通 经典高端商务手机",2080.00,10,"p6.jpg");
  16. Phones[6]=new Phone("联通4G W系列手机",1780.00,13,"p7.jpg");
  17. Phones[7]=new Phone("联通4G 经典手机",1280.00,27,"p8.jpg");
  18. function showPhones(){
  19. var divPro=document.getElementById("show");
  20. divPro.innerHTML="";
  21. for(var i in Phones){
  22. var dlObj=document.createElement("dl");
  23. var content="
    ";
  24. content+="

    $"+Phones[i]["price"]+"

    销量:"+Phones[i]["sale"]+"
    ";
  25. content+="
    "+Phones[i]["desc"]+"
    ";
  26. dlObj.innerHTML=content;
  27. dlObj.className="eachPro";
  28. divPro.appendChild(dlObj);
  29. }
  30. }
  31. function sortByPrice(){
  32. for(var i=0;i<Phones.length;i++){
  33. for(var j=0;j<Phones.length-1-i;j++){
  34. if(Phones[j]["price"]>Phones[j+1]["price"]){
  35. var temp=Phones[j];
  36. Phones[j]=Phones[j+1];
  37. Phones[j+1]=temp;
  38. }
  39. }
  40. }
  41. }
  42. function sortBySale(){
  43. for(var i=0;i<Phones.length;i++){
  44. for(var j=0;j<Phones.length-1-i;j++){
  45. if(Phones[j]["sale"]>Phones[j+1]["sale"]){
  46. var temp=Phones[j];
  47. Phones[j]=Phones[j+1];
  48. Phones[j+1]=temp;
  49. }
  50. }
  51. }
  52. }
  53. function sortPro(){
  54. var selValue=document.getElementById("sort").value;
  55. if(selValue==1){
  56. //按照价格排序数组

  57. }
  58. else if(selValue==2){
  59. //按照销量排序数组

  60. }
  61. //显示数组中保存的产品对象

  62. }
  63. 排序样式:
  64. --请选择--
  65. 价格
  66. 销量
  • Csdn user default icon
    上传中...
    上传图片
    插入图片