简体   繁体   English

在Jquery Ajax中使用选择器和$(this)

[英]Using selectors and $(this) in Jquery Ajax

I have event mouseenter link with get ajax request, I want to get selector $(this) of link and get attribute. 我有事件mouseenter链接与获取ajax请求,我想获得选择器$(this)的链接和获取属性。 I'm use context of setting ajax for AJAX callback. 我使用为AJAX回调设置ajax的context

jQuery(document).ready(function($) {
  var request;
  $('a[rel="bookmark"]').mouseenter(function() { 
  // other stuff
  request = $.ajax({
      dataType: "JSON",
      url: '<?php echo admin_url("admin-ajax.php"); ?>',
      data: {"action": "our_ajax_function", "id": dataId},
      context: $(this).parent().get(0),
      success: function(data){
          // other stuff
          var gettitle = $(this).attr('data-title').replace('Permanent Link to ','');
      }
  })
});

But I get this error 但是我得到了这个错误

Uncaught TypeError: Cannot read property 'replace' of undefined 

I have event mouseenter link with get ajax request, I want to get selector $(this) of link and get attribute. 我有事件mouseenter链接与获取ajax请求,我想获得选择器$(this)的链接和获取属性。 I'm use context of setting ajax for AJAX callback. 我使用为AJAX回调设置ajax的context

jQuery(document).ready(function($) {
  var request;
  $('a[rel="bookmark"]').mouseenter(function() { 
  // other stuff
  request = $.ajax({
      dataType: "JSON",
      url: '<?php echo admin_url("admin-ajax.php"); ?>',
      data: {"action": "our_ajax_function", "id": dataId},
      context: $(this).parent().get(0),
      success: function(data){
          // other stuff
          var gettitle = $(this).attr('data-title').replace('Permanent Link to ','');
      }
  })
});

But I get this error 但是我得到了这个错误

Uncaught TypeError: Cannot read property 'replace' of undefined 

HTML HTML

<ul>
   <li>
    <a href="http://localhost/area-no-kishi/" rel="bookmark" data-title="Permanent Link To Area no Kishi" data-id="4126" target="_blank">Area no Kishi </a>
   </li>
   <li>
    <a href="http://localhost/aria-the-scarlet-ammo-hidan-no-arai/" rel="bookmark" data-title="Permanent Link To Permanent Link to Aria the Scarlet Ammo ( Hidan No Aria )" data-id="1081" target="_blank">Aria the Scarlet Ammo ( Hidan No Aria ) </a>
   </li>
</ul>

try assigning "this" to a variable: 尝试将“this”分配给变量:

jQuery(document).ready(function($) {
  var request;
  $('a[rel="bookmark"]').mouseenter(function() { 
  var that=this;
  // other stuff
  request = $.ajax({
      dataType: "JSON",
      url: '<?php echo admin_url("admin-ajax.php"); ?>',
      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: 在使用HTML5数据属性时,您可以使用data()函数获取或修改jQuery中的data()

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

If you want this inside the callback to refer to the a element (ie the element the handler was bound to), use 如果你想this回调里面指的是a元素(即该元素的处理程序绑定到),使用

context: this

instead of 代替

context: $(this).parent().get(0)

$(this).parent().get(0) selects the parent of the a element, which is a li element, which doesn't seem to have a data-title attribute. $(this).parent().get(0)选择a元素的父元素,它是一个li元素,它似乎没有data-title属性。

From the documentation : 文档

context 上下文
This object will be made the context of all Ajax-related callbacks. 此对象将成为所有与Ajax相关的回调的上下文。 By default, the context is an object that represents the ajax settings used in the call ( $.ajaxSettings merged with the settings passed to $.ajax ). 默认情况下,上下文是一个对象,表示调用中使用的ajax设置( $.ajaxSettings与传递给$.ajax的设置合并)。 For example, specifying a DOM element as the context will make that the context for the complete callback of a request, like so: 例如,将DOM元素指定为上下文将使得请求的完整回调的上下文,如下所示:

 $.ajax({ url: "test.html", context: document.body }).done(function() { $( this ).addClass( "done" ); }); 

See also $(this) inside of AJAX success not working 另见$(this)内部的AJAX成功无效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM