点击事件jQuery,li标签中的href(jquery on click event, a href within li tag)

编程入门 行业动态 更新时间:2024-10-04 07:29:47
点击事件jQuery,li标签中的href(jquery on click event, a href within li tag)

我有以下列方式填写的UL:

$("#dropdownFloorplanGroups").append("<li><a href='#'>" + value.Name + "</a></li>");

这在我的浏览器上正确显示

然而,我似乎无法启动点击事件,然后提醒我在<li></li>设置的名称

我试过这个,但没有被解雇。

$('li a').click(function (e) { alert($(this).find("a").text()); });

jquery点击事件应该是什么?

I have a UL which I populate in the following way:

$("#dropdownFloorplanGroups").append("<li><a href='#'>" + value.Name + "</a></li>");

This displays correctly on my browser

However I can't seem to fire off the click event, which then alerts the name that I have set in the <li></li>

I tried this, but nothing gets fired.

$('li a').click(function (e) { alert($(this).find("a").text()); });

What should the jquery click event be?

最满意答案

你必须使用事件委托:

$('#dropdownFloorplanGroups').on('click', 'li a', function(e) { alert($(this).text()); });

$('li a')不会考虑新创建的元素。

You have to use event delegation:

$('#dropdownFloorplanGroups').on('click', 'li a', function(e) { alert($(this).text()); });

$('li a') won't take into account the newly created elements.

更多推荐

本文发布于:2023-08-07 13:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464094.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事件   标签   jQuery   li   href

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!