开发者

Fetch id of clicked element using .live()

As per suggestions i changed my code to below

<dl id="accRegion">
<#list command.instruments as instrType>            
<dt ><a class="red">${instrType}</a></dt>
<dd >
    <#list command.logDates as logDate>
        <div id="${instrType}"><a class="orange">${logDate}</a></dt>
    </#list>
</dd>
</#list>

but it still gets me the id of first element instead of the clicked one. Also, this dl is an accordion.

Here is the generated html fragment

<dl id="accRegion">
<dt ><a class="red">Stocks</a></dt>
<dd >
        <div id="Stocks"><a class="orange">2011-05-31</a></dt>
</dd>
<dt ><a class="red">Futures</a></dt>

<dd >
        <div id="Futures"><a class="orange">2011-05-31开发者_运维百科</a></dt>
</dd>

Trying below method

    $("#accRegion div").live('click',function(){
    alert($(this).attr("id"));
});


Try this:

$("#accRegion dd dt").live('click',function(e){
    alert($(e.currentTarget).attr("id"));
});



you are using same id multiple times on a page ;whereas its is necessary that id must be unique on a pages; that's your main problem. and there is also alternative method of attr in jQuery v1.6.Yu can use jQuery.prop()

$("#accRegion dd dt").live('click',function(){
    alert($(this).prop("id"));
});

DEMO

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜