问题
Just like we have selectors like ("#id_name"),(".class_name") (":input_type")
..etc in jquery, how can i select title
attribute from an input tag?
Ex:< input type="abc" id="xyz" title="information" >
i need a selector to select title value. Help me.
回答1:
$('input[title="information"]');
回答2:
$('input').attr('title');
@Alex is partially correct, you can have numbers but the id just cannot start with a number.
in your case you would selected the input like this.
$('#xyz').attr('title');
回答3:
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.
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.
$("#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.
$("#p-2[title~='RS4']" ).css("color", "blue");
Hope this helps
回答4:
Use
var titleValue = $('#123').attr('title');
As Alex pointed out, you shouldn't use numeric ids.
回答5:
You use CSS attribute selectors. For your example, it would be like this:
$('input[title="information"]')
See CSS attribute selectors for more information on selecting tags based on attributes.
回答6:
<input type="abc" id="id_123" title="information" >
<script>
var title = $('#id_123').attr('title');
</script>
回答7:
$('input').attr('title')
or $('input[title="whatever"])
回答8:
If you want to query them by title using attribute selector
$('[type=abc]')
You can filter by attributes putting by using [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.
回答9:
Try as follow
$("[title|='Tomorrow']")
来源:https://stackoverflow.com/questions/6844678/how-can-i-select-title-attribute-in-an-input-tag-in-jquery