jQuery 选择器:ID 以什么结尾?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/609382/
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 09:27:03  来源:igfitidea点击:

jQuery Selector: Id Ends With?

jqueryjquery-selectors

提问by Josh Stodola

Is there a selectorthat I can query for elements with an ID that ends with a given string?

是否selector可以查询 ID 以给定字符串结尾的元素?

Say I have a element with an id of ctl00$ContentBody$txtTitle. How can I get this by passing just txtTitle?

假设我有一个 id 为 的元素ctl00$ContentBody$txtTitle。我怎么能通过传递就得到这个txtTitle

回答by Mark Hurd

If you know the element type then: (eg: replace 'element' with 'div')

如果您知道元素类型,则:(例如:将“元素”替换为“div”)

$("element[id$='txtTitle']")

If you don't know the element type:

如果您不知道元素类型:

$("[id$='txtTitle']")

More information available

更多信息可用



// the old way, needs exact ID: document.getElementById("hi").value = "kk";
$(function() {
  $("[id$='txtTitle']").val("zz");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="ctl_blabla_txtTitle" type="text" />

回答by Romain Guidoux

The answer to the question is $("[id$='txtTitle']"), as Mark Hurd answered, but for those who, like me, want to find all the elements with an id which starts witha given string (for example txtTitle), try this (doc) :

问题的答案是$("[id$='txtTitle']")正如 Mark Hurd 所回答的那样,但是对于像我一样,想要找到以给定字符串(例如 txtTitle)开头的 id 的所有元素的人,试试这个(doc):

$("[id^='txtTitle']")

If you want to select elements which id containsa given string (doc) :

如果要选择 id包含给定字符串 ( doc) 的元素:

$("[id*='txtTitle']")

If you want to select elements which id is nota given string (doc) :

如果要选择 id不是给定字符串 ( doc) 的元素:

$("[id!='myValue']")

(it also matches the elements that don't have the specified attribute)

(它还匹配没有指定属性的元素)

If you want to select elements which id contains a given word, delimited by spaces(doc) :

如果要选择 id包含给定单词的元素,以空格( doc)分隔

$("[id~='myValue']")

If you want to select elements which id is equal to a given string or starting with that string followed by a hyphen(doc) :

如果要选择 id等于给定字符串或以该字符串开头后跟连字符( doc) 的元素:

$("[id|='myValue']")

回答by kkyy

Try

尝试

$("element[id$='txtTitle']");

edit: 4 seconds late :P

编辑:迟到 4 秒:P

回答by Scott Evernden

$('element[id$=txtTitle]')

It's not strictly necessary to quote the text fragment you are matching against

引用您要匹配的文本片段并不是绝对必要的

回答by Nick Gilbert

It's safer to add the underscore or $ to the term you're searching for so it's less likely to match other elements which end in the same ID:

将下划线或 $ 添加到您要搜索的术语会更安全,这样就不太可能匹配以相同 ID 结尾的其他元素:

$("element[id$=_txtTitle]")

(where elementis the type of element you're trying to find - eg div, inputetc.

(其中element是您要查找的元素类型 - 例如divinput等等。

(Note, you're suggesting your IDs tend to have $ signs in them, but I think .NET 2 now tends to use underscores in the ID instead, so my example uses an underscore).

(注意,您建议您的 ID 中往往包含 $ 符号,但我认为 .NET 2 现在倾向于在 ID 中使用下划线,因此我的示例使用下划线)。

回答by Anton K

An example: to select all <a>s with ID ending in _edit:

示例:选择<a>ID 以 _edit 结尾的所有s:

jQuery("a[id$=_edit]")

or

或者

jQuery("a[id$='_edit']")

回答by Michael

Since this is ASP.NET, you can simply use the ASP <%= %> tag to print the generated ClientID of txtTitle:

由于这是 ASP.NET,您可以简单地使用 ASP <%= %> 标签打印生成的 txtTitle 的 ClientID:

$('<%= txtTitle.ClientID %>')

This will result in...

这将导致...

$('ctl00$ContentBody$txtTitle')

... when the page is rendered.

...当页面呈现时。

Note: In Visual Studio, Intellisense will yell at you for putting ASP tags in JavaScript. You can ignore this as the result is valid JavaScript.

注意:在 Visual Studio 中,Intellisense 会因为你在 JavaScript 中放置 ASP 标签而大喊大叫。您可以忽略这一点,因为结果是有效的 JavaScript。

回答by pawel

Try this:

尝试这个:

<asp:HiddenField ID="0858674_h" Value="0" runat="server" />

var test = $(this).find('[id*="_h"').val();

回答by neelmeg

In order to find an iframe id ending with "iFrame" within a page containing many iframes.

为了在包含许多 iframe 的页面中找到以“iFrame”结尾的 iframe id。

jQuery(document).ready(function (){     
                  jQuery("iframe").each(function(){                     
                    if( jQuery(this).attr('id').match(/_iFrame/) ) {
                            alert(jQuery(this).attr('id'));

                     }                   
                  });     
         });