使用属性 “title” 将 html 元素链接到 jQuery.click()

Linking html element to jQuery.click() using attribute "title"

本文关键字:链接 jQuery click 元素 html 属性 title      更新时间:2023-09-26

>我有一个html代码:

<a title="intro">INTRO?</a>

我需要在标签上链接一个jQuery点击事件。使用这里给出的解决方案,我编写了以下javascript:

jQuery("a[title='intro']").click(alert("abc"));

但是,网页会在网页加载时发出警报("abc"),而不是在点击标签时发出警报。还要通知上面的代码不在加载函数jQuery(function() {... }内,而是一个单独的函数。

有什么解决方案吗?

您在事件注册期间调用 alert 函数,并将alert返回的值作为单击回调处理程序传递。

相反,您需要传递函数引用作为单击回调,并在函数中添加警报调用

jQuery("a[title='intro']").click(function(){
    alert("a")
});