如何在具有相同 id 属性的所有元素上应用 jquery?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4035570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:32:46  来源:igfitidea点击:

How can i apply jquery on all elements with same id attribute?

jquery

提问by Azfar Niaz

How can i apply jquery on all elements with same id attribute ?

如何在具有相同 id 属性的所有元素上应用 jquery?

i want to apply a focus()and blur()function on a textareaelements that have same id?

我想在具有相同 id的元素上应用 afocus()blur()函数textarea

回答by efkah

The right answer would be

正确答案是

$("[id=yourID]").doSomething()

for any code like

对于任何代码,如

<textarea id="yourID" />
<img id="yourID" />
<div id="yourID" />

yes, i know, this is not valid html but note that we middle-class-developers often have to deal with problems "better" developers made. Check out Microsoft SharePoint for example, where multiple IDs are very common. And we cannot tell our customers that Microsoft did poorly so we cannot help them. We tell them Microsoft did poorly so we have to help them;-)

是的,我知道,这不是有效的 html,但请注意,我们中产阶级开发人员经常不得不处理“更好”的开发人员提出的问题。例如,查看 Microsoft SharePoint,其中多个 ID 非常常见。我们不能告诉我们的客户微软做得不好,所以我们无法帮助他们。我们告诉他们微软做得不好,所以我们必须帮助他们;-)

回答by Dustin Laine

First, you should not have more than one element with the same ID.

首先,您不应拥有多个具有相同ID.

However, you could do this with CSS classes:

但是,您可以使用 CSS 类来做到这一点:

<div>
 <textarea class="Text"></textarea>
 <textarea class="Text"></textarea>
 <textarea class="Text"></textarea>
</div>

<script>
    $(".Text").focus();
    $(".Text").blur();
</script>

回答by Tom Walters

You cannot have more than one element with the same ID, you should use classes, then access them like so.

您不能拥有多个具有相同 ID 的元素,您应该使用类,然后像这样访问它们。

$(".classname").focus()

And if you want to focus on a single ID use

如果您想专注于单个 ID,请使用

$("#idname").focus()

回答by nonopolarity

It is best not to use the same id for 2 elements on the same page. Other forms that sets the textarea nodes can be

最好不要对同一页面上的 2 个元素使用相同的 id。设置textarea节点的其他形式可以是

$('textarea').blur()   // note it sets all textarea elements on the page

or

或者

$('#content textarea, #comment-area textarea').blur()

or if you create a class for these textarea nodes, you can use that too.

或者如果您为这些 textarea 节点创建一个类,您也可以使用它。

回答by straut

This is very simple if you want use `$("#yourID").click(function(){});

如果你想使用 `$("#yourID").click(function(){}); 这很简单。

回答by Vijay Lathiya

use jQuery delegate

使用 jQuery 委托

syntax

句法

jQuery('body').delegate("#your-id","click",function(e){ // do code here });

回答by Darin Dimitrov

You cannot/shouldn't have multiple elements with the same id. You could use a class selector but I would definitely fix the ids problem first.

您不能/不应该有多个具有相同 ID 的元素。您可以使用类选择器,但我肯定会先解决 ids 问题。

回答by mcgrailm

you can't have more than one element with the same id not only is it invalid html but if you trageted it using jquery it would only effect the first one. you should use a class

您不能拥有多个具有相同 id 的元素,这不仅是无效的 html,而且如果您使用 jquery 对它进行处理,它只会影响第一个。你应该使用一个类

$('.class_name').blur();