Tag: this

Javascript – 如何在对象文字的ajax调用中绑定’this’

我有一个对象文字router ,包含一个ajax调用。 我想在ajax调用中调用this.printMovies()其他函数,但这引用了ajax对象。 如何逃避它并使其引用router对象本身? var router = { //… init : function() { this.getData(“api/movies”, “movies”, callback); }, getData : function (url, htmlType, callback) { $.ajax({ url: url, dataType: ‘json’, success: function (response) { if (response && response.length > 0) { this.printMovies(response, callback); //’this’ refers to ajax this.printMovies(response, callback).bind(this) //still doesn’t work } }, error: function […]

调用Javascript函数时,如何设置自定义值“this”?

我正在使用jQuery,并且我有一个用作事件回调的函数,因此在该函数中“this”表示捕获事件的对象。 但是,有一个实例,我想从另一个函数显式调用该函数 – 在这种情况下如何设置函数中的“this”将相等? 例如: function handleEvent(event) { $(this).removeClass(“sad”).addClass(“happy”); } $(“a.sad”).click(handleEvent); // in this case, “this” is the anchor clicked function differentEvent(event) { $(“input.sad”).keydown(e) { doSomeOtherProcessing(); handleEvent(e); // in this case, “this” will be the window object // but I’d like to set it to be, say, the input in question } }

jQuery在if语句中使用’this’

我正在使用if语句来确定元素是否有任何子元素。 如果它没有任何孩子,我只想对该元素做一些事情。 这是我正在尝试做的前提: if ($(“#div a”).children().length > 0){ $(this).hide(); } 因此,如果标签没有子 ,我想对该特定元素(或多个也没有子元素的元素)执行某些操作。 问题是this尚未定义,因为它是一个if语句。 我可能完全错过了一些东西,但我不太清楚如何实现这一目标。 任何意见,将不胜感激

如何使用内联HTML onclick和jQuery获取单击的元素?

我正在用这段代码创建一个标签: $(‘#td’ + id).append(‘ ‘ + file + ”); excluirArquivofunction function excluirArquivo(arquivo) { $.ajax({ type: ‘POST’, url: ‘/erp/proposta/index.php/arquivo/remover/’ + arquivo }); alert($(this)); } 但是excluirArquivo函数里面的这个元素正在返回Window对象。 如何获取excluirArquivo中的单击元素(标记)?