2010年10月9日土曜日

SCRIPTタグのDOM要素位置の取得

同じ画面にまったく同じ(JavaScriptを含む)HTML要素を出力して、
それぞれのブロックが別々に動作するようにしたい場合の方法。

<div id="p1" class="portlet">
<script type="text/javascript">
(function(){
var container = $("script:last").parents("div:first");
var hello = function() {
alert(this.id);
};
$(function(){
container.click(hello);
});
})();
</script>
</div>

<!-- 上のDIVと中身は全く同じ -->
<div id="p2" class="portlet">
<script type="text/javascript">
(function(){
var container = $("script:last").parents("div:first");
var hello = function() {
alert(this.id);
};
$(function(){
container.click(hello);
});
})();
</script>

</div>

0 件のコメント:

コメントを投稿