如何在 jquery 的输入标签中选择“title”属性?

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

How can i select 'title' attribute in an input tag in jquery?

jqueryattributescss-selectorstitle

提问by Rahul_2289

Just like we have selectors like ("#id_name"),(".class_name") (":input_type")..etc in jquery, how can i select titleattribute from an input tag?

就像我们("#id_name"),(".class_name") (":input_type")在 jquery 中有像..etc这样的选择器一样,我如何title从输入标签中选择属性?

Ex:< input type="abc" id="xyz" title="information" >

前任:< input type="abc" id="xyz" title="information" >

i need a selector to select title value. Help me.

我需要一个选择器来选择标题值。帮我。

回答by Alex Ackerman

$('input[title="information"]');

回答by locrizak

$('input').attr('title');

@Alex is partially correct, you can have numbers but the id just cannot start with a number.

@Alex 部分正确,您可以有数字,但 ID 不能以数字开头。

in your case you would selected the input like this.

在你的情况下,你会选择这样的输入。

$('#xyz').attr('title');

回答by locrizak

I know this is an old post but here is your solution.

我知道这是一个旧帖子,但这是您的解决方案。

The var method can be very handy but you can do this directly via jQuery without var.

var 方法可能非常方便,但您可以直接通过 jQuery 执行此操作,而无需 var。

Say if you had a p (paragraph) with an id="p-2" and the title attribute value is RS4, and you like to colorize that paragraph in blue. You use the following.

假设您有一个 id="p-2" 的 ap(段落)并且标题属性值为 RS4,并且您希望将该段落着色为蓝色。您使用以下内容。

$("#p-2[title='RS4']" ).css("color", "blue");

Another method if the title attribute value has the word RS4 contained in it in the case title="Audi RS4 is fast", then you can use ~= method.

另一种方法,如果title="Audi RS4 is fast"的情况下title属性值中包含RS4这个词,那么可以使用~=方法。

$("#p-2[title~='RS4']" ).css("color", "blue");

Hope this helps

希望这可以帮助

回答by gregspurrier

You use CSS attribute selectors. For your example, it would be like this:

您使用 CSS 属性选择器。对于您的示例,它会是这样的:

$('input[title="information"]')

See CSS attribute selectorsfor more information on selecting tags based on attributes.

有关基于属性选择标签的更多信息,请参阅CSS 属性选择器

回答by Jonas Stensved

Use

var titleValue = $('#123').attr('title');

As Alex pointed out, you shouldn't use numeric ids.

正如亚历克斯指出的那样,您不应该使用数字 ID。

回答by fatnjazzy

<input type="abc" id="id_123" title="information" >
<script>
var title = $('#id_123').attr('title');
</script>

回答by Phil

$('input').attr('title')or $('input[title="whatever"])

$('input').attr('title')或者 $('input[title="whatever"])

回答by Luiz Sócrate

If you want to query them by title using attribute selector

如果要使用属性选择器按标题查询它们

$('[type=abc]')

You can filter by attributes putting by using [ATTRIBUTE=VALUE].

您可以使用 [ATTRIBUTE=VALUE] 按属性进行过滤。

If you want to return the attribute them use

如果你想返回他们使用的属性

$(SELECTOR).attr('type');

Also, it is not valid to start and Id value with a number, you should fix that.

此外,以数字开头和 Id 值是无效的,您应该解决这个问题。

回答by Arshid KV

Try as follow

尝试如下

$("[title|='Tomorrow']")