gpt4 book ai didi

javascript - 在 ul javascript/jquery 中返回点击的 li 类

转载 作者:行者123 更新时间:2023-11-30 06:59:46 25 4
gpt4 key购买 nike

我的代码(html 页面):

<nav>
<ul>
<li id="homeLink"><a href="#">Home</a></li>
<li id="rekenLink"><a href="#">Rekenmachine</a></li>
<li id="bakkerLink"><a href="#">Parkeergarage</a></li>
<li id="garageLink"><a href="#">Bij de bakker</a></li>
<ul>
</nav>

背后的javascript/jquery:

$(function () {
$("ul").click(function () {
// here I want to get the clicked id of the li (e.g. bakkerLink)
});
});

我该怎么做?

最佳答案

使用 .on()带有签名的方法 $(common_parent).on(event_name, filter_selector, event_listener).

演示:http://jsfiddle.net/gLhbA/

$(function() {
$("ul").on("click", "li", function() {
// here I want to get the clicked id of the li (e.g. bakkerLink)
var id = this.id;
alert(id);
});
});

另一种方法是将事件绑定(bind)到 li 而不是 ul:

$(function() {
$("li").click(function() {
// here I want to get the clicked id of the li (e.g. bakkerLink)
var id = this.id;
alert(id);
});
});

关于javascript - 在 ul javascript/jquery 中返回点击的 li 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9726245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com