这仅在单击锚点时触发,并将元素存储id为变量.
$("a").on("click", function (e) {
// Id of the element that was clicked
var elementId = $(this).attr("id");
});
Run Code Online (Sandbox Code Playgroud)
如果要在单击锚点时阻止默认操作,请将其添加到函数末尾:
e.preventDefault(); return false;
Run Code Online (Sandbox Code Playgroud)
如果要将锚点标记为已点击以供将来参考,请尝试以下操作:
$("a").on("click", function (e) {
// Id of the element that was clicked
var elementId = $(this).attr("id");
$(this).data("clicked", true);
});
Run Code Online (Sandbox Code Playgroud)
使用jquery,检查是否已使用
$(element).data("clicked") // returns True or False
Run Code Online (Sandbox Code Playgroud)
如果您想要监听所有被点击的内容并想要查看它是否是锚点,请尝试:
$("*").not("body").on("click", function (e) {
if ($(this).is("a")) {
// Id of the element that was clicked
var elementId = $(this).attr("id");
} else {
// An anchor was not clicked, do something else
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7731 次 |
| 最近记录: |