簡體   English   中英

該功能適用​​於Chrome,但在IE和Firefox中無法正常運行

[英]Function works fine with chrome but not in IE and Firefox

我在firefox和IE中運行js腳本時遇到問題,但在Chrome上運行正常。 到目前為止,我還沒有滿意的答案。 我必須建立一份訂購餐單,而且我必須能夠補充營養。 用戶必須單擊此處:

<a href="#"  class="ajoutPlat"><img src="images/plusdetour.png" width="25" height="25" alt="plusdetour"/>

我應該將一個隱藏的div克隆的下拉列表添加到我的html中。

HTML

<table border="0">
<thead>
    <tr>
        <th>Supplements disponibles</th>

    </tr>
    <tr>
        <th>________________</th>

    </tr>
</thead>

<tr>
    <td><p>Plat supplementaire</p></td>
    <td><a href="#"  class="ajoutPlat"><img src="images/plusdetour.png" width="25" height="25" alt="plusdetour"/></a>
    </td>
</tr>
<tr>
    <td><p>Garniture supplementaire</p></td>
    <td><a href="#" class="ajoutGarniture"><img src="images/plusdetour.png" width="25" height="25" alt="plusdetour"/></a>
    </td>
</tr>
<tr>
    <td><p>Pain Supplemaentaire</p></td>
    <td><a href="#" class="ajoutPain"><img src="images/plusdetour.png" width="25" height="25" alt="plusdetour"/></a>
    </td>
</tr
<tr>
    <td><p>Potage</p></td>
    <td><a href="#" class="ajoutPotage"><img src="images/plusdetour.png" width="25" height="25" alt="plusdetour"/></a>
    </td>
</tr>
<?php
foreach ($elementsSupplements as $element) {

    echo '<tr>'
    . '<td>'
    . '<p>' . $element . '</p>'
    . '</td>'
    . '<td>'
    . '<a href="#" class="ajout' . $element . '"><img src="images/plusdetour.png" width="25" height="25" alt="plusdetour"/></a>'
    . '</td>'
    ;
}
?>

</table>
<br/>

JS

$(function() {
var z = 0;
var y = 0;
var x = 0;
var w = 0;
var lim1 = "Le nombre de plats , garnitures ou potages supplementaires est limité a 1";
var lim10 = "Le supplément selectionné ne sera pas rajouté , vous avez atteint la limite fixée a 10";
var choix = "Le potage n'est pas disponible si vous avez pris un plat et/ou une garniture supplémentaire";

$(".ajoutPlat").live('click', function() {
    if (z < 10 && x < 1 && w < 1) {
        plat = $('.plat:first').clone();
        $("<tr><td><p>+1 Plat</p></td><td id='addplat" + z + "'></td></tr></br>").appendTo("#supplements");
        plat.appendTo("#addplat" + z);
        z++;
        x++;
    } else {
        if (z > 10) {
            alert(lim10);
        } else if (x > 0) {
            alert(lim1);
        }
    }

});
$(".ajoutPotage").live('click', function() {
    if (z < 10) {
        if (w < 1) {
            if (x === 0 && y === 0) {
                plat = $('.potage:first').clone();
                $("<tr><td><p>+1 Potage</p></td><td id='addPotage" + z + "'></td></tr></br>").appendTo("#supplements");
                plat.appendTo("#addPotage" + z);
                z++;
                w++;
            } else {
                alert(choix);
            }
        } else {
            alert(lim1);
        }
    } else {
        alert(lim10);
    }

});

$(".ajoutGarniture").live('click', function() {
    if (z < 10 && y < 1 && w < 1) {
        garniture = $('.garniture:first').clone();
        $("<tr><td><p>+1 Garniture</p></td><td id='addGarniture" + z + "'></td></tr></br>").appendTo("#supplements");
        garniture.appendTo("#addGarniture" + z);
        z++;
        y++;
    } else {
        if (z > 10) {
            alert(lim10);
        } else if (y > 0) {
            alert(lim1);
        }
    }

});

$(".ajoutJournal-Eure-Info").live('click', function() {
    if (z < 10)
    {
        jei = $('.Journal-Eure-Info:first').clone();
        $("<tr><td><p>+1 Journal</p></td><td id='addJournal-Eure-Info" + z + "' class='cellCentre'></td></tr></br>").appendTo("#supplements");
        jei.appendTo("#addJournal-Eure-Info" + z);
        z++;
    }
    else
    {
        alert(lim10);
    }
});

$(".ajoutJournal-La-dépêche").live('click', function() {
    if (z < 10)
    {
        jld = $('.Journal-La-dépêche:first').clone();
        $("<tr><td><p>+1 Journal</p></td><td id='addJournal-La-dépêche" + z + "' class='cellCentre'></td></tr></br>").appendTo("#supplements");
        jld.appendTo("#addJournal-La-dépêche" + z);
        z++;
    }
    else
    {
        alert(lim10);
    }
});

$(".ajoutPanier-de-fruits").live('click', function() {
    if (z < 10)
    {
        pdf = $('.Panier-de-fruits:first').clone();
        $("<tr><td><p>+1 Panier</p></td><td id='addPanier-de-fruits" + z + "' class='cellCentre'></td></tr></br>").appendTo("#supplements");
        pdf.appendTo("#addPanier-de-fruits" + z);
        z++;
    }
    else
    {
        alert(lim10);
    }
});
$(".ajoutCroissant").live('click', function() {
    if (z < 10)
    {
        crs = $('.Croissant:first').clone();
        $("<tr><td><p>+1 Croissant</p></td><td id='addCroissant" + z + "' class='cellCentre'></td></tr></br>").appendTo("#supplements");
        crs.appendTo("#addCroissant" + z);
        z++;
    }
    else
    {
        alert(lim10);
    }
});


$(".ajoutPain-au-Chocolat").live('click', function() {
    if (z < 10)
    {
        pac = $('.Pain-au-Chocolat:first').clone();
        $("<tr><td><p>+1 Pain choco</p></td><td id='addPain-au-Chocolat" + z + "' class='cellCentre'></td></tr></br>").appendTo("#supplements");
        pac.appendTo("#addPain-au-Chocolat" + z);
        z++;
    }
    else
    {
        alert(lim10);
    }
});

$(".ajoutPain").live('click', function() {
    if (z < 10)
    {
        pan = $('.pain:first').clone();
        $("<tr><td><p>+1 Pain</p></td><td id='addPain" + z + "'></td></tr></br>").appendTo("#supplements");
        pan.appendTo("#addPain" + z);
        z++;
    }
    else
    {
        alert(lim10);
    }
});
});

到目前為止,它工作正常。我想知道為什么它不再工作。


原來問題是我的jQuery中的$ .browser來解決問題,我用

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"</script>
<script src="http://code.jquery.com/jquery-migrate-1.0.0.js"></script>

現在,一切都可以使用FF IE chrome和Safari正常運行。 使用FF插件Firebug發現了問題。

第一 :不要live使用。 使用委托。 jQuery在1.7版本(現在已經有4年歷史了)中棄用了.live()方法。 反過來,事件冒泡是處理異步創建的節點的自然方法。

$(function() {
var z = 0;
var y = 0;
var x = 0;
var w = 0;
var lim1 = "Le nombre de plats , garnitures ou potages supplementaires est limité a 1";
var lim10 = "Le supplément selectionné ne sera pas rajouté , vous avez atteint la limite fixée a 10";
var choix = "Le potage n'est pas disponible si vous avez pris un plat et/ou une garniture supplémentaire";

$(document).on('click',".ajoutPlat", function() {
    ...
});

$(document).on('click', ".ajoutPotage", function() {
    ...
});

$(document).on('click', ".ajoutGarniture", function() {
    ...
});

$(document).on('click', ".ajoutJournal-Eure-Info", function() {
    ...
});

$(document).on('click',".ajoutJournal-La-dépêche", function() {
    ...
});

$(document).on('click',".ajoutPanier-de-fruits", function() {
    ...
});

$(document).on('click', ".ajoutCroissant", function() {
   ...
});


$(document).on('click',".ajoutPain-au-Chocolat", function() {
    ...
});

$(document).on('click',".ajoutPain", function() {
    ...
});

第二 :注意不要嘗試附加到尚不存在的對象。 例如

plat = $('.plat:first').clone();
$("<tr><td><p>+1 Plat</p></td><td id='addplat" + z + "'></td></tr></br>").appendTo("#supplements");
plat.appendTo("#addplat" + z);

取決於瀏覽器和星星的位置,是否可能不起作用。 正確的方法是

var plat = $('.plat:first').clone();
var newtr= $('<tr></tr>');
var addplat=$('<td id="addplat' + z + '"></td>');
plat.appendTo(addplat);
newtr.append('<td><p>+1 Plat</p></td>');
newtr.append(addplat);
newtr.appendTo("#supplements");

你看,你也不需要做的jQuery去搜索DOM "#addplat" + z (這可能不是由那么DOM),因為你已經知道哪個節點是你的addplat對象,甚至將它添加到前DOM。

第三,我不確定ID中的特殊字符。 它們應該工作,但我寧願將所有內容保留為ASCII字符。 例如,使用類ajoutJournal-La-depeche代替ajoutJournal-La-dépêche 只是為了安全起見。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM