Can';不能让(这个)jQuery选择器工作

Can';不能让(这个)jQuery选择器工作,jquery,jquery-selectors,this,Jquery,Jquery Selectors,This,我的页面上有一个名为“a.add_mycratet”的重复类。 此脚本仅针对单击的实例(This)并处理它 这是我的JS: jQuery(document).ready(function(){ jQuery(document).on('click', "a.add_mycrate", function(event) { event.preventDefault(); var title = jQuery(this).attr('data-title'); var art

我的页面上有一个名为“a.add_mycratet”的重复类。 此脚本仅针对单击的实例(This)并处理它

这是我的JS:

jQuery(document).ready(function(){
jQuery(document).on('click', "a.add_mycrate", function(event) {
    event.preventDefault();

    var title = jQuery(this).attr('data-title');
    var artwork = jQuery(this).attr('data-artwork');
    var stream = jQuery(this).attr('data-stream');
    var download = jQuery(this).attr('data-download');

    var data = {
                'action': 'addToCrate',
                'security': jQuery( '#crate-nonce' ).val(),
                'podtitle' : title,
                'podartwork' : artwork,
                'podstream' : stream,
                'podsave' : download
               };                  

    jQuery.post(myCrate.ajaxurl, data, function(response) {

        jQuery('a.add_mycrate', this).html(response);

        alert(response);
    });              
  });
});
这是页面上实际链接的呈现方式(如果有帮助):

我知道我一定是做错了,但我在这里四处看看,尝试过的所有方法都不起作用。有什么建议吗

附言与问题无关,但我使用的是“jQuery”而不是“$”,因为它正在wordpress网站上运行,以防有人怀疑。

几个问题:

位于
$内。post
回调不是您想象的那样。它的作用域上下文与外部事件处理程序中的
this
不同。 在ajax外部存储对该的引用,以便在其内部使用

var $this = jQuery(this);
jQuery.post(myCrate.ajaxurl, data, function(response) {
        $this.html(response);
});
这也将解决以下问题:

jQuery('a.add_mycrate', this).html(response);
第二个论点是“上下文”。如果假设
是元素,则上下文参数使其与以下内容相同:

jQuery(this).find('a.add_mycrate');
因此,您将查找其内部的元素。这两个问题都是通过将元素引用存储在ajax之外并将html直接插入到存储的元素中来解决的

编辑:如何在wordpress noConflict()中使用$,而不必对所有内容使用
jQuery

jQuery(document).ready(function($){
    // can use `$` for all your code inside the ready handler
    // when you pass it in as argument
})
几个问题:

位于
$内。post
回调不是您想象的那样。它的作用域上下文与外部事件处理程序中的
this
不同。 在ajax外部存储对该的引用,以便在其内部使用

var $this = jQuery(this);
jQuery.post(myCrate.ajaxurl, data, function(response) {
        $this.html(response);
});
这也将解决以下问题:

jQuery('a.add_mycrate', this).html(response);
第二个论点是“上下文”。如果假设
是元素,则上下文参数使其与以下内容相同:

jQuery(this).find('a.add_mycrate');
因此,您将查找其内部的元素。这两个问题都是通过将元素引用存储在ajax之外并将html直接插入到存储的元素中来解决的

编辑:如何在wordpress noConflict()中使用$,而不必对所有内容使用
jQuery

jQuery(document).ready(function($){
    // can use `$` for all your code inside the ready handler
    // when you pass it in as argument
})

post
之前将
this
放入名为
elm
的变量中,然后更改
elm
的html,如下所示

jQuery(document).ready(function(){
    jQuery(document).on('click', "a.add_mycrate", function(event) {
        event.preventDefault();

        var title = jQuery(this).attr('data-title');
        var artwork = jQuery(this).attr('data-artwork');
        var stream = jQuery(this).attr('data-stream');
        var download = jQuery(this).attr('data-download');
        var elm = this;

        var data = {
            'action': 'addToCrate',
            'security': jQuery( '#crate-nonce' ).val(),
            'podtitle' : title,
            'podartwork' : artwork,
            'podstream' : stream,
            'podsave' : download
        };                  

        jQuery.post(myCrate.ajaxurl, data, function(response) {
            jQuery(elm).html(response);
            alert(response);
        });              
    });
});

post
之前将
this
放入名为
elm
的变量中,然后更改
elm
的html,如下所示

jQuery(document).ready(function(){
    jQuery(document).on('click', "a.add_mycrate", function(event) {
        event.preventDefault();

        var title = jQuery(this).attr('data-title');
        var artwork = jQuery(this).attr('data-artwork');
        var stream = jQuery(this).attr('data-stream');
        var download = jQuery(this).attr('data-download');
        var elm = this;

        var data = {
            'action': 'addToCrate',
            'security': jQuery( '#crate-nonce' ).val(),
            'podtitle' : title,
            'podartwork' : artwork,
            'podstream' : stream,
            'podsave' : download
        };                  

        jQuery.post(myCrate.ajaxurl, data, function(response) {
            jQuery(elm).html(response);
            alert(response);
        });              
    });
});

这个
不是元素
这个
不是元素精彩的解释,是的,它当然有效。谢谢Charlietfl;)请参阅关于如何使用
$
的额外说明是的,我在几乎所有其他函数中都使用了$wrap,只是使用jQuery编写了此函数,没有特殊原因。;)精彩的解释,是的,当然有效。谢谢Charlietfl;)请参阅关于如何使用
$
的额外说明是的,我在几乎所有其他函数中都使用了$wrap,只是使用jQuery编写了此函数,没有特殊原因。;)