使用属性"title"将html元素链接到jQuery.click(). [英] Linking html element to jQuery.click() using attribute "title"

查看:113
本文介绍了使用属性"title"将html元素链接到jQuery.click().的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个html代码:

I have a html code:

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

我需要在标签上链接一个jQuery click事件.使用此处给出的解决方案,我编写了以下javascript:

I need to link a jQuery click event on the tag. Using the solution given here I wrote the following javascript:

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

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

However the page is alerting ("abc") on page load rather than on clicking the tag. Also to inform that the above code is NOT inside the load function jQuery(function() {... } and is a separate function.

有解决方案吗?

推荐答案

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

You are invoking the alert function during the event registration and is passing the value returned by the alert as the click callback handler.

相反,您需要传递函数引用作为点击回调,并且可以在函数内添加警报调用

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")
});

这篇关于使用属性"title"将html元素链接到jQuery.click().的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆