jquery - 如何在委托(delegate)事件中获取 $(this)

标签 jquery html

我的页面包含以下链接

<a id="21561" class="delete_item" href="#" name="baseMenu">
<img class="icons" src="../images/delete.gif">
</a>

<a id="219350" class="delete_item" href="#" name="menuOptions">
<img class="icons" src="images/delete.gif">
</a>

这两个链接之间的唯一区别是 name 属性。

我遇到的问题是,这些链接是通过 $.post() 方法动态注入(inject)到 HTML 文档中的

我试图在单击删除链接时获取 name 属性,但是当我使用 $(this) 属性结合使用它时,它似乎不起作用。

谁能帮我在点击事件中获取 .delete_item 链接的 $(this) 属性。

任何帮助将不胜感激。提前致谢

$(document).on("click",  $(".delete_item") ,   function(){


var test = $(".delete_item", this);
console.log(test);
return false; //We just want get the value for now
no need to parse the rest of the code. We now it works


//rest of the ajax code .......
 )

最佳答案

您不需要在委托(delegate)事件定义中使用 jQuery 函数调用,我认为这是问题的根源。一旦你解决了这个问题,你就应该能够使用 $(this)

一个例子:

$(document).on("click",  ".delete_item" ,   function(){

var test = $(this);
console.log(test);
return false; //We just want get the value for now no need to parse the rest of the code. We now it works

//rest of the ajax code .......
 });

关于jquery - 如何在委托(delegate)事件中获取 $(this),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26170728/

相关文章:

javascript - jQuery UI 日期选择器将不会显示 - 包含完整代码

javascript - 通过 jquery 使用外部源更改 SVG 的颜色

jquery - 在 Highcharts 的多 Pane 中调整图表大小/隐藏图表

html - sup 标签在 title 标签内不起作用

javascript - 三个HTML选择哪些选项依赖于前面的

javascript - jQuery.grep 与 Array.filter 的性能对比

javascript - JQuery 单击编辑帖子并选择 id 不起作用

jquery - <li> 标签中的嵌套 <a> 标签点击区域不起作用

javascript - 如何使用一张提交表单执行两个单独的操作

javascript - JQuery 创建按钮但不可点击