简体   繁体   English

jquery只选择第一个元素

[英]jquery only selecting first element

I'm trying to add button and click listener to every save span on a page. 我正在尝试添加按钮并单击侦听器到页面上的每个保存范围。 I do: 我做:

$("#save").button().click(function(){
   currentSlide++;
   loadPage();
}

loadPage clears my dialog box and loads the next slide into the box. loadPage清除我的对话框并将下一张幻灯片加载到框中。 If I don't add that code to the load page function, only the first save button is assigned the button and click listener. 如果我不将该代码添加到加载页面功能,则只为第一个保存按钮分配按钮并单击监听器。 Why do i need to assign the listener every time. 为什么我每次都需要分配监听器。 Shouldn't jQuery assign the button and click listener to all #save? 不应该jQuery分配按钮并单击所有#save的监听器?

All the slides are stored on the page. 所有幻灯片都存储在页面上。 Load page takes the hidden content and appends it to the dialog box. 加载页面获取隐藏的内容并将其附加到对话框。

Id must be unique for all elements!!! Id必须是所有元素的唯一!!!

You have defined an ID selector for you bind to click function. 您已为绑定到单击功能定义了ID选择器 There should only be one ID for each element therefor $('#save') will only bind to one element (the first one). 因此每个元素只应有一个ID $('#save')只会绑定到一个元素(第一个元素)。 If you want to bind to all spans used in this way then add a class to each span and reference it this way, using a class selector 如果要绑定到以这种方式使用的所有跨度,则向每个跨度添加一个类并使用类选择器以这种方式引用它

$('span.mysave')

First, you should not have more than one element on a page sharing the same id attribute. 首先,在页面上共享相同的id属性不应该有多个元素。

The id attribute is reserved to be a unique identifier for a single element in a page. id属性保留为页面中单个元素的唯一标识符。

I would recommend adding a class to your elements instead of relying on matching ids. 我建议在元素中添加一个类,而不是依赖匹配的id。

Secondly, if you are dynamically adding elements to the page, use .live('click', ...) instead of .click() to be able to work with current and future elements added to the page. 其次,如果要动态地向页面添加元素,请使用.live('click', ...)而不是.click()来处理添加到页面的当前未来元素。

// note selecting class ".save" instead of id "#save"
$(".save").button().live('click', function(){
   currentSlide++;
   loadPage();
}

# means id and id='save' is valid for only one element per document (one id 's value can't be used more than once ). #表示idid='save'仅对每个文档一个元素有效(一个id的值不能 多次使用 )。 Use class='save' instead and use 请改用class='save'并使用

$(".save").button().click(function(){

save is an ID and as such should only be once on a page. save是一个ID,因此只能在页面上使用一次。 $('#save') maps to document.getElementById('save') which returns one element. $('#save')映射到document.getElementById('save') ,它返回一个元素。

If you are loading loading content you should use live or delegate . 如果要加载内容,则应使用livedelegate

#save is an ID. #save是一个ID。 IDs are supposed to be unique. ID应该是唯一的。 jQuery assumes this and only gets the 1st element. jQuery假设这个并且只获得第一个元素。 Instead of an ID, use a class. 而不是ID,使用一个类。

Think of the name, class, and ID attributes like this: 想想名称,类和ID属性,如下所示:

  • Elements can have a one name, but they don't have to be unique. 元素可以有一个名称,但它们不必是唯一的。 Multiple elements can have the same name 多个元素可以具有相同的名称
  • Elements can be in or more classes. 元素可以是或多个类。 Multiple elements can be in the same class. 多个元素可以在同一个类中。
  • IDs is a unique ID for that element. ID是该元素的唯一ID。

I have a feeling that you're using the id save more than once on the page. 我有你正在使用的ID的感觉save不止一次在页面上更多。 At the least, using #save as a jQuery selector will only select one element due to it being an ID. 至少,使用#save作为jQuery选择器只会选择一个元素,因为它是一个ID。

Replace id="save" with class="save" and use .save to select it with jQuery - classes can be used multiple times, and even multiple classes used on one element. class="save"替换id="save"并使用.save用jQuery选择它 - 类可以多次使用,甚至在一个元素上使用多个类。

Unlike the other replies, I am guessing that by 'first' save button you mean that you are replacing the contents of an ancestor of the element with id="save" when you loadPage() . 与其他回复不同,我猜测,通过'first'保存按钮,您意味着在loadPage()时用id="save"替换元素祖先的内容。

If this is the case the original 'save button' element that jQuery bound to when you called click() has been destroyed and replaced with a new one that jQuery doesn't know about. 如果是这种情况,jQuery在调用click()时绑定的原始“保存按钮”元素已被销毁,并被jQuery不知道的新元素替换。 Only the elements that were selected when you first created the selector are bound to; 只有第一次创建选择器时选择的元素才被绑定; it does not update when you change the set of elements that would match. 更改匹配的元素集时,它不会更新。

So after calling loadPage() you would have to call click() to bind the function again (and also button() again, since that changed the old element so would need to be directed to update the new one too). 因此,在调用loadPage()您必须再次调用click()来绑定函数(以及再次button() ,因为这会更改旧元素,因此需要指示更新新元素)。

If you want to bind an event to “any element now or in the future that has id save ” then what you want is event delegation ( delegate() , or its questionable cousin live() ): 如果你想将一个事件绑定到“现在或将来有id save任何元素”,那么你想要的是事件委托( delegate() ,或者它有问题的堂兄live() ):

$(document.body).delegate('#save', 'click', function(){
    currentSlide++;
    loadPage();
});

Though this doesn't allow you to re-call button() . 虽然这不允许你重新调用button()

What others have said is true, jQuery will return the first ID. 别人说的是真的,jQuery会返回第一个ID。 But, that's not always the case. 但是,情况并非总是如此。 Run this test code to see: 运行此测试代码以查看:

<script type="text/javascript" src="/scripts/jquery-1.6.1.min.js"></script>
<script type="text/javascript">
    $(function() {
        $("div").click(function() {
            //$("#a", $("div")).remove(); // 1
            $("#a").remove();             // 2
        });
    });
</script>

<div style="border: solid 1px black;">
    <p id="a">I am the first paragraph.</p>
    <p id="a">I am the second paragraph.</p>
</div>

When you run line 2, it takes away the first #a. 当你运行第2行时,它会夺走第一个#a。 Running line 1 removes them both. 运行第1行将它们都删除。 So if you can't change the markup to use classes instead, sub selecting might be the answer. 因此,如果您无法更改标记以使用类,则可以选择子选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM