Are there any more than just a Id and Class -selectors to work with when using Jquery?
For example:
<div id="myId" class="myClass"></div>
<script>
$('.myClass').click(function() {
alert(this.id); //alerts my "myId"
});
</script>
I want something like:
<div id="myId" data-id="myDataId" class="myClass"></div>
<script>
$('.myClass').click(function() {
alert(this.dataId);
});
</script>
You can different selectors like:
$( "div[data-id='myDataId']" );
and then:
$(this).attr("data-id")
or .data() (if you use newer jQuery >= 1.4.3)
$(this).data("id")
You can read it here: http://api.jquery.com/attribute-contains-selector/ http://api.jquery.com/category/selectors/
You can do:
<div id="myId" data-id="myDataId" class="myClass"></div>
<script>
$('.myClass').click(function() {
alert($(this).data("id"));
});
</script>
$("[data-id=myDataId]")
JSfiddle http://jsfiddle.net/sjj41box/1/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.