jQuery: get data attribute

后端 未结 5 2263
慢半拍i
慢半拍i 2020-11-27 03:18

In my html I have a span element:

This is a

相关标签:
5条回答
  • 2020-11-27 03:59

    Change IDs and data attributes as you wish!

      <select id="selectVehicle">
           <option value="1" data-year="2011">Mazda</option>
           <option value="2" data-year="2015">Honda</option>
           <option value="3" data-year="2008">Mercedes</option>
           <option value="4" data-year="2005">Toyota</option>
      </select>
    
    $("#selectVehicle").change(function () {
         alert($(this).find(':selected').data("year"));
    });
    

    Here is the working example: https://jsfiddle.net/ed5axgvk/1/

    0 讨论(0)
  • 2020-11-27 04:11

    1. Try this: .attr()

      $('.field').hover(function () {
        var value=$(this).attr('data-fullText');
      $(this).html(value);
    });
    

    DEMO 1: http://jsfiddle.net/hsakapandit/Jn4V3/

    2. Try this: .data()

    $('.field').hover(function () {
        var value=$(this).data('fulltext');
      $(this).html(value);
    });
    

    DEMO 2: http://jsfiddle.net/hsakapandit/Jn4V3/1/

    0 讨论(0)
  • 2020-11-27 04:17

    This is what I came up with:

    		$(document).ready(function(){
    		
    		$(".fc-event").each(function(){
    		
    			console.log(this.attributes['data'].nodeValue)	
    		});
        
        });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <div id='external-events'>
    			<h4>Booking</h4>
    			<div class='fc-event' data='00:30:00' >30 Mins</div>
    			<div class='fc-event' data='00:45:00' >45 Mins</div>
    </div>

    0 讨论(0)
  • 2020-11-27 04:20

    You could use the .attr() function:

    $(this).attr('data-fullText')
    

    or if you lowercase the attribute name:

    data-fulltext="This is a span element"
    

    then you could use the .data() function:

    $(this).data('fulltext')
    

    The .data() function expects and works only with lowercase attribute names.

    0 讨论(0)
  • 2020-11-27 04:21

    This works for me

    $('.someclass').click(function() {
        $varName = $(this).data('fulltext');
        console.log($varName);
    });
    
    0 讨论(0)
提交回复
热议问题