2011-12-06 17 views
0

リンクに動的に追加されたクラスがあり、その親のクラスがpdSelectedSwatchで、選択(クリック)に追加されたときに、divに.showをトリガーしようとしています。動的要素のjqueryをトリガーする

コード例:

<li class="pdSelectedSwatch"> 
    <a href="javascript:void(0)" class="outOfStock"> 
     <img class="outOfStock" src="/outofstock.gif" > 
    </a> 
</li> 



if ($("a.outOfStock").parent().hasClass("pdSelectedSwatch")) { 
    $(".stockMessage").show(); 
} 

しかし、私はそれが正常に動作しない場合は、と私は私の構文を賭けているがオフになっています。

outOfStockクラスはxmlファイルの値によって決まり、そのファイルに基づいてリンクのクラスを決定する関数がいくつか存在することに注意してください。

また、このサイトでは、jQuery 1.4.2を使用していますが、これはthsi時には更新されません。

ありがとうございました。

+0

HTMLコードとは何ですか? – locrizak

答えて

1

セレクタが正しいことを確認するために、いくつかのHTMLを使用できますか?

ユーザー編集にREPONSE

//if the xml is parsed and used on page load, then just find the offending a.outOfStock elements 
$(document).ready(){ 
//.find assumes the stock message is a child of the a.outOfStock Element. 
// i am inferring there could be multiple a.outOfStocks on a page, and the message is localized. 
$(".pdSelectedSwatch > a.outOfStock").find(".stockMessage").fadeIn("slow"); 
} 

//a global ajax complete event, you must check the uri used to be sure it is the ajax call you care about! 
// you should probably add a 'complete' event to the original ajax call if you have control of it 
// if you're still reading, then you may not be in control of the xml ajax call. 
// you can still listen for a jQuery complete event. this only works if the xml ajax call used jQuery as well. 
$('selector for whatever called the ajax').ajaxComplete(function(e, xhr, settings) { 
    if (settings.url == 'ajax/uri/you/used/to/get/the.xml') { 
    $(".pdSelectedSwatch > a.outOfStock").find(".stockMessage").fadeIn("slow"); 
    } 
}); 

// as of 1.4.2 
$(".pdSelectedSwatch > a").delegate(".outOfStock","click", function(event){ 
     $(".stockMessage").show(); 
     }); 
+0

上記の編集を参照してください - jQuery 1.4.2を使用して – Jason

+0

XMLファイルが処理されたときにトリガーするイベントを使用できます。それはページの負荷ですか? (私は現在、 'クリック'イベントを使って推測しています)... sidenote、 'a.outOfStock'リンクがデフォルトのhrefを起動しないようにしますか? – DefyGravity

+0

残念ながら、これらの関数にはかなりの量のjsが含まれています。だから、私は現時点で彼らにタップするのを避けようとしており、特定の条件が満たされればdivを表示したいだけです。 – Jason

関連する問題