开发者

Accessing in Javascript link passed from PHP

So I have this PHP code

    </div>
    <div id="topmusicartisttitle">Top 5 Music Artist</div>
    <div class="edit"><a href="" name="topartistsedit">edit</a></div>
    <div id="topmusicartistlist">
    <ul>
    ...

that basically passes a list of stuff and I want to be able to click on this a href in javascript but I don't want it to go anywhere I just want to catch the click and handle it. So to start I have:

   $('a[name=birthdayedit').live('click',function(e){
  e.preventDefault();
  });

But this doesn't seem to work. I checked firebug and the href and the name are there (obviously), but the click isn't registered in Javascript and it still redirects. I assume live is the function to use since this is p开发者_如何学Pythonretty much dynamically created content. Any one know what I'm doing wrong?


Change

$('a[name=birthdayedit')

to

$('a[name=topartistsedit]')

or change the name in your HTML.


I see several mistakes:

  1. There is not closing bracket (a ]) in a[name=birthdayedit'
  2. The name attribute in the HTML is different from that which the JS references.


It's easier to change your HTML to use an id like this:

<div class="edit"><a href="#" id="topartistsedit">edit</a></div>

And, then you can capture the click like this:

$("#topartistsedit").click(function() {
    // do what you want in the click function
    return(false);   // returning false stops default behavior and propagation
});

or if the content is created dynamically after page load:

$("#topartistsedit").live("click", function() {
    // do what you want in the click function
    return(false);   // returning false stops default behavior and propagation
});

Example here: http://jsfiddle.net/jfriend00/8FgFP/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜