Home > Web Front-end > JS Tutorial > jQuery solution for adding click events to multiple items

jQuery solution for adding click events to multiple items

黄舟
Release: 2017-06-27 10:05:11
Original
1696 people have browsed it

<div><a id="x1" title="..."></a><a id="x2" title="..."></a>...</div>
Copy after login

How to add events to all a tags in a divThe parameter is its own title

This should work

$(function(){
    $("div a").click(function(){
        var oParam=$(this).attr("title");
        alert(oParam);//得到点击的a标签的title值
    });
});
Copy after login

Under normal circumstances It is best to use an id for the div here, and use the form |$("#id a") to facilitate positioning.

$(document).ready(function(){$("div a").click(function(){ //点击事件就click 鼠标放上就mouseover,
Copy after login

You can change it yourself

$("#input").val($(this).attr("title")); 
//#input就是要你放进去的那个文本框的ID,里面是被点击a的title值});
});
Copy after login

Although it is handwritten, it passed the test under IE8.

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function(){
//思路是,找到a标签集合遍历
$("div a").each(function(){
//注册事件,事件名称取自title属性值
$(this).bind($(this).attr("title"),function(){alert(this)})
})
}) </script>
</head>
<body>
<div>
<!- 鼠标划入事件 ->
<a href="#" title="mouseover">把鼠标移入我这里</a>
<p/><p/>
<!- 鼠标单击事件 ->
<a href="#" title="click">单击我这里</a>
</div>
</body>
</html> 不知道你是不是要这个效果?
Copy after login
$(function(){ $("div>a[title]").bind("click",function(){ //都添加了单击事件。 })});
Copy after login
$("a").click(function(){
    var _title = $(this).attr("title");
});
Copy after login

The above is the detailed content of jQuery solution for adding click events to multiple items. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template