$this -不能找到正确的jQuery选择器
$this - can't find the correct jQuery selector
由于我的html结构过于复杂(wordpress评论系统),我很难找到在页面上显示一些div的选择器。
我没有把它们全部粘贴在这里,而是创建了一个更简化的布局版本,并创建了几个fiddle。
- 这是显示所有div的完整页面:
- 这就是我想要的工作方式。基本上,回复框和相关的现有评论是隐藏的,直到"回复"被点击,然后隐藏的div向下滑动。我已经包括了我的"非工作"JS在这个小提琴。希望有人能告诉我哪里做错了? https://jsfiddle.net/yf3oagx7/
(function ($) {
$('.single-f3ed-reply').hide();
$('.f3ed-reply').hide();
$('a.this-reply').click(function () {
$('.single-f3ed-reply').hide();
$(this).parents().next('.single-f3ed-reply').slideDown('fast');
$(this).parents().next('.f3ed-reply').slideDown('fast');
return false;
});
})(jQuery);
.parents()
返回所有所选元素之上的元素。您不希望这样,您只希望在包含div/wrapper的位置向上移动。
.next()
返回下一项(经过过滤),这在parents()
向上到最近的div (closest
),然后再向下(find
)到你想要的项目:
$(this).closest(".stream-wrap").find('.single-f3ed-reply').slideDown('fast');
$(this).closest(".stream-wrap").find('.f3ed-reply').slideDown('fast');
https://jsfiddle.net/yf3oagx7/1/快速破解:https://jsfiddle.net/e25zvfyg/2/
使用:$(this).closest('article').find('.single-f3ed-reply').slideDown('fast');
$(this).closest('article').find('.f3ed-reply').slideDown('fast');
你可以试试——
(function ($) {
$('.single-f3ed-reply').hide();
$('.f3ed-reply').hide();
$('a.this-reply').click(function () {
$('.single-f3ed-reply').hide();
$(this).parent().parent().parent().find('.single-f3ed-reply').slideDown('fast');
$(this).parent().parent().parent().find('.f3ed-reply').slideDown('fast');
return false;
});
})(jQuery);
你也可以使用jquery的close()函数来实现。
(function ($) {
$('.single-f3ed-reply').hide();
$('.f3ed-reply').hide();
$('a.this-reply').click(function () {
$('.single-f3ed-reply').hide();
$(this).closest('.stream-wrap').find('.single-f3ed-reply').slideDown('fast');
$(this).closest('.stream-wrap').find('.f3ed-reply').slideDown('fast');
return false;
});
})(jQuery);
相关文章:
- jQuery选择器无法正常工作
- 将jquery选择器转换为数组
- JavaScriptDOM正常工作时JQuery选择器不工作
- 在我的情况下,使用带有变量失败的 jquery 选择器
- 换行符插入jquery选择器
- 使用javascript"这个“;用于jquery选择器
- jQuery选择器缓存问题
- jQuery 选择器,所有跨度都在 DIV 内
- JQuery选择器:如果同级具有.class,则选择td
- jQuery选择器不识别任何动态创建的HTML输入函数
- 循环遍历元素jquery选择器
- jQuery选择器错误:无法识别的表达式
- 自定义jquery选择器属性未在模拟器中激发
- Jquery选择器为空,我应该使用哪个作用域
- jQuery选择器就是用这个构建的
- jQuery 选择器混淆
- 将jQuery选择器与“this”相结合
- jQuery选择器在脚本中不起作用,但在控制台中工作
- jQuery 选择器中的简单 jQuery 语法和串联
- 全局 JQuery 选择器缓存以提高性能