Using selectors and $(this) in Jquery Ajax

前端 未结 2 798
萌比男神i
萌比男神i 2021-01-23 14:44

I have event mouseenter link with get ajax request, I want to get selector $(this) of link and get attribute. I\'m use context of setting

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-23 15:47

    try assigning "this" to a variable:

    jQuery(document).ready(function($) {
      var request;
      $('a[rel="bookmark"]').mouseenter(function() { 
      var that=this;
      // other stuff
      request = $.ajax({
          dataType: "JSON",
          url: '',
          data: {"action": "our_ajax_function", "id": dataId},
          success: function(data){
              // other stuff
              var gettitle = $(that).data('title','Permanent Link to ');
          }
      })
    });
    

    also while using the HTML5 Data Attribute you can get or modify the data in jQuery with the data() function:

    $(that).data('title','Permanent Link to '); //sets the "data-title" of the selected element as "Permanent Link to "
    

提交回复
热议问题