jQuery - 如何找到具有特定 id 的元素?

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

jQuery - how can I find the element with a certain id?

jquery

提问by AndreMiranda

I have a table and each of its tdhas a unique idthat corresponds to some time intervals (0800til 0830... 0830til 0900and so on).

我有一个表,每个表td都有一个唯一的 id,对应于一些时间间隔(0800til 0830... 0830til0900等等)。

I have an input text where the user will type the time intervals they want to block.

我有一个输入文本,用户将在其中输入他们想要阻止的时间间隔。

If they type an interval that doesn't exist in my table, in other words, if they type an interval that doesn't correspond to any of my td's id's, I want to show an alert saying something like this interval is not available for blocking.

如果键入不中我的表中,换句话说,如果他们键入不符合我的任何一个间隔的间隔td's id's,我想显示一个警告说类似this interval is not available for blocking

But, I'm having difficulty to find this id.

但是,我很难找到这个 ID。

I'm doing this:

我这样做:

    var horaInicial = $("#horaInicial").val().split(':')[0] + $("#horaInicial").val().split(':')[1]; // this is remover the ":" from a formatted hour

    var verificaHorario = $("#tbIntervalos").find("td").attr("id", horaInicial);

But this verificaHorariois actually setting all my td's to this horaInicial id.

但这verificaHorario实际上是将我所有的 td 设置为这个 horaInicial id。

How can I find the id in my table and if it doesn't exist show some alert?

如何在我的表中找到 id,如果它不存在则显示一些警报?

回答by Jesse Rusak

If you're trying to find an element by id, you don't need to search the table only - it should be unique on the page, and so you should be able to use:

如果您尝试通过 id 查找元素,则无需仅搜索表格 - 它在页面上应该是唯一的,因此您应该能够使用:

var verificaHorario = $('#' + horaInicial);

If you need to search only in the table for whatever reason, you can use:

如果您出于某种原因只需要在表中搜索,您可以使用:

var verificaHorario = $("#tbIntervalos").find("td#" + horaInicial)

回答by cletus

I don't know if this solves your problem but instead of:

我不知道这是否能解决您的问题,但不是:

$("#tbIntervalos").find("td").attr("id", horaInicial);

you can just do:

你可以这样做:

$("#tbIntervalos td#" + horaInicial);

回答by kgiannakakis

This

这个

var verificaHorario = $("#tbIntervalos").find("#" + horaInicial);

will find you the td that needs to be blocked.

会找到你需要屏蔽的td。

Actually this will also do:

实际上这也可以:

var verificaHorario = $("#" + horaInicial);

Testing for the size() of the wrapped set will answer your question regarding the existence of the id.

测试包装集的 size() 将回答您关于 id 是否存在的问题。

回答by bhupen

This is one more option to find the element for above question

这是查找上述问题元素的另一种选择

$("#tbIntervalos").find('td[id="'+horaInicial+'"]')

$("#tbIntervalos").find('td[id="'+horaInicial+'"]')

回答by Simon

As all html ids are unique in a valid html document why not search for the ID directly? If you're concerned if they type in an id that isn't a table then you can inspect the tag type that way?

由于所有 html id 在有效的 html 文档中都是唯一的,为什么不直接搜索该 ID?如果您担心他们输入的 id 不是表格,那么您可以通过这种方式检查标签类型吗?

Just an idea!

只是一个想法!

S