简体   繁体   English

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

[英]Linking html element to jQuery.click() using attribute “title”

I have a html code: 我有一个HTML代码:

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

I need to link a jQuery click event on the tag. 我需要在标记上链接jQuery click事件。 Using the solution given here I wrote the following javascript: 使用这里给出的解决方案我写了以下javascript:

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

However the page is alerting ("abc") on page load rather than on clicking the tag. 但是,页面在页面加载时发出警报(“abc”)而不是单击标记。 Also to inform that the above code is NOT inside the load function jQuery(function() {... } and is a separate function. 另外要通知上面的代码不在加载函数jQuery(function() {... }并且是一个单独的函数。

Any solutions pls? 有什么解决方案吗?

You are invoking the alert function during the event registration and is passing the value returned by the alert as the click callback handler. 您在事件注册期间调用alert功能,并将alert返回的值作为单击回调处理程序传递。

Instead you need to pass a function reference as the click callback and within the function you can add the alert call 相反,您需要传递一个函数引用作为单击回调,并且在函数内可以添加警报调用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM