关于jquery:DOM操作不起作用

DOM manipulation does not work

是的,我知道这个标题并没有真正的帮助,但这是确切的问题。由于某些原因(我不知道),我无法在AJAX-Request的beforeSend函数中操纵HTML。

这是HTML:

1
2
3
    <span id="observer-eye" original-title="Observe this Summoner" data-id="2">
        <img alt="" src="/lolreports/img/icons/observer_eye.png">
    </span>

这是AJAX请求:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$.ajax({
    type: 'post',
    dataType: 'json',
    url: webroot + 'summoners/observe',
    data: 'data[Summoner][id]=' + id,
    beforeSend: function(XMLHttpRequest) {
        $(this).parent().empty();
    },
    success: function(data) {
        if(data.status == 'success') {
            $(this).find('img').attr("src", webroot + 'img/icons/observed_eye.png"');
        }
    }
});

我的目标是将ajax-loader.gif放在我想用$(this).find('img')。attr(" src",webroot'img / icons / ajax- loader.gif"');-什么都没发生。我测试了许多方法来处理图像或跨度,但是什么也没发生。我想知道beforeSend是否曾经执行过,我用alert()测试了。操作

我不知道在哪里搜索,希望您能为我提供帮助。


请勿使用$(this),因为它没有正确引用您想要的元素。当然,有很多方法可以执行此操作,请尝试

1
$("#observer-eye").find('img').attr("src", webroot + 'img/icons/observed_eye.png"');

在此之前,请勿使用$(this),而应使用

1
$('the-intended-selector').parent.empty()