0

私は単純なhmtlとjqueryを使用して小さなプロジェクトを作成していますが、その問題については現時点では難しいと予想しています。htmlを超えると、次のような単純な画像が表示されます。

<img src="color.png" alt="color" width="44" height="46" /> 
<img src="gray.png" alt="gray" width="44" height="46" />

しかし、altが同じである場合もあるので、jqueryで「チェック関数」を作成して、画像のaltを比較およびチェックし、それらがアラートを実行するのと等しいかどうかを確認します。私はここに来ましたが、うまくいきません

    jQuery(document).ready(function(){

        var grayBall = $("img[alt*='gray']");
        var colorBall = $("img[alt*='color']");

        if(colorBall.attr('alt') = grayBall.attr('alt')){
        alert("They are matching !");
        };  
    });

jqueryをhtmlに追加するようなものはすべて問題ありませんが、この関数だけは機能しません。

4

2 に答える 2

2

(割り当て)==の代わりに(等式)を使用してみてください=

if(colorBall.attr('alt') == grayBall.attr('alt'))
{
    alert("They are matching !");
};  

アップデートこれを試しましたか?1つのimgのaltタグを変更し、スクリプトのセレクターも変更します。

<img src="color.png" alt="color" width="44" height="46" /> 
<img src="gray.png" alt="color" width="44" height="46" />


jQuery(document).ready(function(){

    var grayBall = $("img[alt*='color']");
    var colorBall = $("img[alt*='color']");

    if(colorBall.attr('alt') == grayBall.attr('alt')) {
        alert("They are matching !");
    };  
});
于 2012-09-12T17:37:08.083 に答える
0

あなたはこれを試すことができます

HTML

<img src="gray.png" alt="gray" width="44" height="46" />
<img src="color.png" alt="color" width="44" height="46" />
<img src="gray.png" alt="gray" width="44" height="46" />
<img src="gray.png" width="44" height="46" />

JS

jQuery(document).ready(function(){
    var exists = {};
    $("img[alt]").each(function(){
        var alt = $(this).attr('alt');
        if (exists[alt]) alert($(this).attr('alt')+" is matching !");
        else exists[alt] = true;
    });
});​

デモ

于 2012-09-12T18:05:17.240 に答える