开发者

How to call same JQuery function on click of elements having same ids

I Have many elements having same is but with different data,I want to call same JQuery function开发者_开发问答 on click of each element.How can I do this?


You cannot have multiple ID's in your HTML markup.

It would be an invalid markup. When querying for $('#foobar') and there are five elements which have that id, you would only get the first instance. So even if there would be a way (...) to apply code to all of those nodes, don't do it.

Use Classnames when you want to "combine" some elements.


Using the same ID on more than one element is not allowed. Use classes for that.

<div class="myClass">..</div>
<div class="myClass">..</div>

$(".myClass").click(function() {...});


try with

jQuery ("div,span,p.myClass").click(function() {

//your code
});

Reference


When operating with id's can create lot of redundancy it is better to operate with classes but if your classes are getting repeated than it could be a bit tricky to handle i would recommend you to use some custom attributes for this

Some sample code for you.

    var spnActive = document.createElement("span");
    $(spnActive).attr('isExpColSpan', 'true');
    $('#dvContainer').find('span[isExpColSpan="true"]').

In the second line i have added an custom attribute in the next line i have shown how to use this attribute.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜