Jquery 为什么我的选择器没有';你找不到我的班级吗?
我的目标是通过单击来删除图像 我有这个js:Jquery 为什么我的选择器没有';你找不到我的班级吗?,jquery,Jquery,我的目标是通过单击来删除图像 我有这个js: $('.imageSelected').on('click', function (event) { console.log('ok'); $this.remove(); }); 这个html: <li class="imageSelected"> <a href="#"> <div class="img-container" style="backg
$('.imageSelected').on('click', function (event)
{
console.log('ok');
$this.remove();
});
这个html:
<li class="imageSelected">
<a href="#">
<div class="img-container" style="background-image:url({{ asset('bundles/mybundle/images/neige.jpg') }});">
<div></div>
</div>
</a>
</li>
但它从来没有进入我的js,你知道为什么吗 您应该使用$(this)
或this
获取当前单击的项目
$(function(){
$('.imageSelected').on('click', function (event){
$(this).remove();
// or this.remove();
});
});
这是一个工作样本
编写jQuery代码时,this
或$(this)
变量的值将根据代码的上下文/范围而改变。因此,将this赋值给一个局部变量,并将其用于其他事情,这始终是一个好主意
例如
$(function(){
$('.imageSelected').on('click', function (event){
var _this=$(this);
//Use _this as needed
_this.css( { color : 'red' });
$.post("UrlToDelete", { id :"someIdFromCurrentElement",function(res) {
_this.fadeOut(100,function(){
_this.remove();
});
});
});
});
您应该使用$(this)
或this
获取当前单击的项目
$(function(){
$('.imageSelected').on('click', function (event){
$(this).remove();
// or this.remove();
});
});
这是一个工作样本
编写jQuery代码时,this
或$(this)
变量的值将根据代码的上下文/范围而改变。因此,将this赋值给一个局部变量,并将其用于其他事情,这始终是一个好主意
例如
$(function(){
$('.imageSelected').on('click', function (event){
var _this=$(this);
//Use _this as needed
_this.css( { color : 'red' });
$.post("UrlToDelete", { id :"someIdFromCurrentElement",function(res) {
_this.fadeOut(100,function(){
_this.remove();
});
});
});
});
正如这里当前定义的,
$this
是一个未定义的新变量。必须使用此
转到当前元素。因此,如果您想在它们上使用jQuery函数,可以将其包装为$(this)
您的解决方案是:
$('.imageSelected').on('click', function(event) {
console.log('ok');
$(this).remove();
});
正如这里当前定义的,
$this
是一个未定义的新变量。必须使用此
转到当前元素。因此,如果您想在它们上使用jQuery函数,可以将其包装为$(this)
您的解决方案是:
$('.imageSelected').on('click', function(event) {
console.log('ok');
$(this).remove();
});
我认为您的元素是动态生成的。您可能需要活动授权。试着像下面这样
$(document).on('click', '.imageSelected', function () {
$(this).remove();
});
我认为您的元素是动态生成的。您可能需要活动授权。试着像下面这样
$(document).on('click', '.imageSelected', function () {
$(this).remove();
});
给定您的代码,它有一个javascript错误,阻止您显示警报或进一步运行jquery。但是,代码中的问题是$this,而这里使用它的正确形式是$(this),它解决了这个问题。我已经在fiddle中添加了修改过的代码,所以请在这里查看您的代码
*https://jsfiddle.net/raghucse2010/c4jjzhvw/7/*
给定您的代码,它有一个javascript错误,阻止您显示警报或进一步运行jquery。但是,代码中的问题是$this,而这里使用它的正确形式是$(this),它解决了这个问题。我已经在fiddle中添加了修改过的代码,所以请在这里查看您的代码
*https://jsfiddle.net/raghucse2010/c4jjzhvw/7/*
我认为
这个.remove()
也是无效的。是吗?是的,这只是一个输入错误,我忘了,但事实是,任何console.log()都会出现,或者出现任何错误。this
将返回HTML元素,$(this)
将返回this的jQuery包装版本。在他的用例中,删除这两个选项都有效(this.remove()
)。对于进一步的操作,我们通常应该使用$(this)
,这是对象的jQuery包装版本。我认为this.remove()
也无效。是吗?是的,这只是一个输入错误,我忘了,但事实是,任何console.log()都会出现,或者出现任何错误。this
将返回HTML元素,$(this)
将返回this的jQuery包装版本。在他的用例中,删除这两个选项都有效(this.remove()
)。对于进一步的操作,我们通常应该使用$(this)
,这是对象的jQuery包装版本。谢谢,但这只是一个输入错误,我忘记了,但事实是,任何console.log()显示,或任何错误,所以它都不会进入事件。谢谢,但这只是一个输入错误,我忘记了,但事实是,任何console.log()显示,或任何错误,因此它不会进入事件。