Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript obj和$this返回未定义_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript obj和$this返回未定义

Javascript obj和$this返回未定义,javascript,jquery,ajax,Javascript,Jquery,Ajax,我正在尝试更改链接的锚文本,并通过id更改其href,以下是我的javascript: $('.like_status').click(function(event) { // bind function to submit event of form event.preventDefault(); $.ajax({ url: $(this).attr('href'), success: function(responseText) {

我正在尝试更改链接的锚文本,并通过id更改其href,以下是我的javascript:

$('.like_status').click(function(event) { // bind function to submit event of form
    event.preventDefault();
    $.ajax({
        url: $(this).attr('href'),
        success: function(responseText) {
            $(this).attr("href", "<?php echo Config::get('URL'); ?>dashboard/unlike_status/" + $(this).attr('id'));
            alert($(this).attr('id'));
            $(this).text('Unlike');

        }
    });
});
$('.like_status')。单击(函数(事件){//绑定函数以提交表单的事件
event.preventDefault();
$.ajax({
url:$(this.attr('href'),
成功:函数(responseText){
$(this.attr(“href”,“dashboard/status/”+$(this.attr('id'));
警报($(this.attr('id'));
$(this.text('不象');
}
});
});
我尝试过使用obj,但也不起作用,下面是我的href:

<span><i class="fa fa-thumbs-up"></i>&nbsp;<a class="like_status" id="<?php echo System::escape($status->timeline_status_id); ?>" href="<?php echo Config::get('URL'); ?>dashboard/like_status/<?php echo System::escape($status->timeline_status_id); ?>"> <?php echo System::translate("Like"); ?></a>&nbsp;</span>

由于作用域和闭包的原因,我建议在函数的开头创建变量,而不是依赖于

// Using the more recent ON method instead of the
// "old-school" .click method
$('.like_status').on('click', function() {
    var $link = $(this);
    event.preventDefault();
    $.ajax({
        url: $link.attr('href'),
        success: function(responseText) {
            $link.attr("href", "<?php echo Config::get('URL'); ?>dashboard/unlike_status/" + $link.attr('id'));
            alert($link.attr('id'));
            $link.text('Unlike');

        }
    });
});
//使用较新的ON方法而不是
//“老派”。点击方法
$('.like_status')。在('单击',函数()上){
var$link=$(此);
event.preventDefault();
$.ajax({
url:$link.attr('href'),
成功:函数(responseText){
$link.attr(“href”,“dashboard/status/”+$link.attr('id'));
警报($link.attr('id'));
$link.text('不同');
}
});
});
我用$作为jQuery对象的前缀,虽然在本例中为负,但通过设置jQuery调用的变量
$(this)
,我不必每次在其他地方调用它


我建议您阅读一些介绍您遇到此问题的原因的文章。

您可以使用
event.target

$('.like_status').click(function(event) { // bind function to submit event of form
    event.preventDefault();
    $.ajax({
        url: $(event.target).attr('href'),
        success: function(responseText) {
            $(event.target).attr("href", "<?php echo Config::get('URL'); ?>dashboard/unlike_status/" + $(event.target).attr('id'));
            alert($(event.target).attr('id'));
            $(event.target).text('Unlike');

        }
    });
});
$('.like_status')。单击(函数(事件){//绑定函数以提交表单的事件
event.preventDefault();
$.ajax({
url:$(event.target.attr('href'),
成功:函数(responseText){
$(event.target.attr(“href”,“dashboard/status/”+$(event.target.attr('id'));
警报($(event.target.attr('id'));
$(event.target).text('不象');
}
});
});

正如评论者所建议的,在ajax函数之外添加对元素的引用,然后使用该引用代替
$(this)

$('.like_status')。单击(函数(事件){//绑定函数以提交表单的事件
event.preventDefault();
var$link=$(this);//添加引用
$.ajax({
url:$link.attr('href'),
成功:函数(responseText){
$link.attr(“href”,“dashboard/status/”+$link.attr('id'));
警报($link.attr('id'));
$link.text('不同');
}
});
});

这个
在ajax成功中并不是指dom对象,而是指xhr对象。您需要捕获jQuery元素以在ajax成功函数中使用。很抱歉,我如何解决这个问题,我仍在学习做这件事:(设置
上下文
或将其缓存为
var$this=$(这个)
作用域和闭包都与
这个
无关(双关语).Scopes是关于源代码的,而
this
是一个运行时问题。本文没有解释OP的问题:每个函数在运行时都有自己对
this
的绑定。但是您可以轻松地将
成功
处理程序的
this
绑定到与
单击
处理程序相同的值如果问题是关于作用域或闭包的,则不可能。首选的解决方案是使用箭头函数,前提是环境支持它。然后,
在对象中引用时发生更改,并且不再是链接(在他的情况下),而是它所在的对象,这是非常奇怪的(在他创建的对象范围内)。我肯定这不是一个闭包问题,我只是在讨论中添加了闭包,因为它与范围具有相似的特性,因此在这个学习示例中将两者结合在一起是一种很好的学习体验。“那么,当在对象中引用时,
这个
会发生变化,这很奇怪。”-嗯,不,这正是我的观点。
this
的值可以在运行时更改,scope不能。请注意,在JavaScript中没有对象或对象范围内的东西。任何函数都可以在任何时候附加到任何对象并独立于它调用,从而导致
this的值不同
。但是,闭包的特性不受影响,函数的范围也不受影响。
$('.like_status').click(function(event) { // bind function to submit event of form
    event.preventDefault();
    var $link = $(this); //add a reference
    $.ajax({
        url: $link.attr('href'),
        success: function(responseText) {
            $link.attr("href", "<?php echo Config::get('URL'); ?>dashboard/unlike_status/" + $link.attr('id'));
            alert($link.attr('id'));
            $link.text('Unlike');

        }
    });
});