如何使用jQuery选择多个元素

时间:2021-10-27 14:31:36

I'm new to JQuery/Javascript etc... based on the following article: How to make an anchor tag refer to nothing?

我是JQuery / Javascript等的新手...基于以下文章:如何使锚标记引用什么?

I would like to apply the java function to several id's. Can we not make the function execute for classes as opposed to ids?

我想将java函数应用于几个id。我们不能让函数执行类而不是ids吗?

<span class="style1" id="myid">Link</span> 
<span class="style1" id="myid">Link</span>
<span class="style1" id="myid">Link</span>
<span class="style1" id="myid">Link</span>
<span class="style1" id="myid">Link</span>

$('myid').click(function() { 
    /* put your code here */ 
}); 

Basically as above, how do I execute the function above for ALL of the links? Is this possible? Thanks in advance.

基本上如上所述,如何为所有链接执行上述功能?这可能吗?提前致谢。

3 个解决方案

#1


13  

Use the following

使用以下内容

$('.style1').click(function() {      
    /* put your code here */  
}); 

This adds a click handler to all elements with class containing style1. You should not have duplicate IDs

这为包含style1的类的所有元素添加了一个单击处理程序。您不应该有重复的ID

#2


23  

you should name the IDs uniquely,

你应该唯一地命名ID,

<span class="style1" id="myid1">Link</span> 
<span class="style1" id="myid2">Link</span>
<span class="style1" id="myid3">Link</span>
<span class="style1" id="myid4">Link</span>
<span class="style1" id="myid5">Link</span>

then use this code

然后使用此代码

$('#myid1,#myid2,#myid3,#myid4,#myid5').click(function() { 
    /* put your code here */ 
}); 

#3


2  

First off, IDs should be unique. You should not have multiple elements with the same ID.

首先,ID应该是唯一的。您不应该有多个具有相同ID的元素。

To select by ID in jQuery use the # character. $('#myid'). This will get the first element with that ID, as there should only be one (you can kinda cheat by doing $('[id="myid"]') to get get multiple elements with the same ID).

要在jQuery中按ID选择,请使用#character。 $( '#身份识别码')。这将获得具有该ID的第一个元素,因为应该只有一个(您可以通过执行$('[id =“myid”]')来获取具有相同ID的多个元素)。

I suggest using a class to select all of your links. Classes are selected using the . character.

我建议使用课程来选择所有链接。使用。选择类。字符。

$('.style1').click(function(){});

#1


13  

Use the following

使用以下内容

$('.style1').click(function() {      
    /* put your code here */  
}); 

This adds a click handler to all elements with class containing style1. You should not have duplicate IDs

这为包含style1的类的所有元素添加了一个单击处理程序。您不应该有重复的ID

#2


23  

you should name the IDs uniquely,

你应该唯一地命名ID,

<span class="style1" id="myid1">Link</span> 
<span class="style1" id="myid2">Link</span>
<span class="style1" id="myid3">Link</span>
<span class="style1" id="myid4">Link</span>
<span class="style1" id="myid5">Link</span>

then use this code

然后使用此代码

$('#myid1,#myid2,#myid3,#myid4,#myid5').click(function() { 
    /* put your code here */ 
}); 

#3


2  

First off, IDs should be unique. You should not have multiple elements with the same ID.

首先,ID应该是唯一的。您不应该有多个具有相同ID的元素。

To select by ID in jQuery use the # character. $('#myid'). This will get the first element with that ID, as there should only be one (you can kinda cheat by doing $('[id="myid"]') to get get multiple elements with the same ID).

要在jQuery中按ID选择,请使用#character。 $( '#身份识别码')。这将获得具有该ID的第一个元素,因为应该只有一个(您可以通过执行$('[id =“myid”]')来获取具有相同ID的多个元素)。

I suggest using a class to select all of your links. Classes are selected using the . character.

我建议使用课程来选择所有链接。使用。选择类。字符。

$('.style1').click(function(){});