从PHP传递的Javascript访问链接

Accessing in Javascript link passed from PHP

本文关键字:Javascript 访问 链接 PHP      更新时间:2023-09-26

PHP代码

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

传递了一个列表我希望能够点击这个javascript中的href但我不希望它去任何地方我只想捕获点击并处理它。首先输入:

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

但这似乎不起作用。我检查了firebug和href和名称在那里(显然),但点击没有在Javascript中注册,它仍然重定向。我假设live是要使用的函数,因为这几乎是动态创建的内容。有人知道我哪里做错了吗?

变化

$('a[name=birthdayedit')

$('a[name=topartistsedit]')

我发现了几个错误:

  1. a[name=birthdayedit'
  2. 中没有右括号(])
  3. HTML中的name属性与JS中的name属性不同引用。

将HTML更改为使用这样的id更容易:

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

然后你可以像这样捕获点击:

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

或者如果内容是在页面加载后动态创建的:

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

示例:http://jsfiddle.net/jfriend00/8FgFP/