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

jQuery solution for adding click events to multiple items

黄舟
黄舟Original
2017-06-27 10:05:111627browse

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

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

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,

You can change it yourself

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

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> 不知道你是不是要这个效果?
$(function(){ $("div>a[title]").bind("click",function(){ //都添加了单击事件。 })});
$("a").click(function(){
    var _title = $(this).attr("title");
});

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!

Statement:
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