开发者

.delegate() not binding to anchor elements tag

I’m trying to use the delegate function and not succeeding, here is my html…

<td class="ms-vb-icon">
    <a tabindex="-1" href="/ACKNOWLEDGEMENT%20OF%20DEBT%20-%20SCOTLAND.doc"
                    onclick="return DispEx(this,event,'TRUE','FALSE','TRUE','SharePoint.O开发者_Python百科penDocuments.3','0','SharePoint.OpenDocuments','','','','41','1','0','0x7fffffffffffffff')">
    <img border="0" alt="ACKNOWLEDGEMENT OF DEBT - SCOTLAND.doc" title="ACKNOWLEDGEMENT OF DEBT - SCOTLAND.doc" src="/_layouts/images/icdoc.gif"></a>
</td>

This is generated for me by SharePoint, there’s a lot more in the table which may be a factor to my problem, please see here for the full listing. The html for the table (id onetidDoclibViewTbl0) is generated after the page has loaded; via an ajax call.

My attempt at using the delegate function...

$('#onetidDoclibViewTbl0').delegate('tr td.ms-vb-icon a', 'click', function(event)  {
    alert(‘document anchor clicked’);
});

I even tried this which didn’t work either

$('body').delegate('a', 'click', function() {
    alert('this really should work?');
});


Most of your links have an onclick attribute that has a return false;. This prevents the event from bubbling up the DOM, which the delegate method relies on to function correctly.


You're right... that really should work. I took your code and put it in a jsFiddle. That seems to be working fine. Perhaps it is related to the version of jQuery you are using? delegate was added relatively recently...

Also, the body element would need to exist at the point at which the code is run. You are probably best off doing as hunter suggested and wrapping it in a document ready callback.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜