Javascript jQuery 发现不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2787896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:55:23  来源:igfitidea点击:

jQuery find not working

javascriptjquery

提问by fire

This is my HTML:

这是我的 HTML:

        <p class="first">blah blah <a href="" class="more">read more</a></p>
        <div class="read_more">
            <p>more text</p>
        </div>

And javascript:

和 javascript:

$(document).ready(function(){
  $('a.more').click(function(){
    $(this).find('.read_more').slideDown();
    return false;
  });
});

Doesn't seem to do anything (read_more is set to display: none) any ideas?

似乎没有做任何事情(read_more 设置为显示:无)任何想法?

回答by Sarfraz

Try this:

尝试这个:

$(document).ready(function(){ $('a.more').click(function(){ $(this).parent().next().find('.read_more').slideDown(); return false; }); });

$(document).ready(function(){ $('a.more').click(function(){ $(this).parent().next().find('.read_more').slideDown() ; 返回假; }); });

Update:

更新:

Here is the demo :)

这是演示:)

Code:

代码:

$(document).ready(function(){
  $('a.more').click(function(){
    $(this).parents().find('.read_more').slideDown('slow');
    return false;
  });
});

You could also do:

你也可以这样做:

$(document).ready(function(){
  $('a.more').click(function(){
    $('.read_more').slideDown('slow');
    return false;
  });
});

Or this:

或这个:

$(document).ready(function(){
  $('a.more').click(function(){
    $(this).parent().next().slideDown('slow');
    return false;
  });
});

回答by Nalum

.find(..)looks for the selector inside the current element.

.find(..)在当前元素内寻找选择器。

What you might want is

你可能想要的是

$(document).ready(function(){
    $('a.more').click(function(){
        $(this).parent().parent().find('.read_more').slideDown();
        return false;
    });
});

Edit

编辑

Added another .parent()as the <a>is inside <p>and .read_moreis not in the <p>

另一个新增.parent()<a>里面<p>,并.read_more没有在<p>

回答by Ali Habibzadeh

$(this) refers to a.more which doesnt not have a child called .read_more thats why it doesnt work

$(this) 指的是 a.more 没有名为 .read_more 的孩子,这就是为什么它不起作用

回答by Tyler Swartzenburg

One potential cause of find() not being able to work is that the element you are looking for hasn't loaded when you call it.

find() 无法工作的一个潜在原因是您正在查找的元素在您调用它时尚未加载。

In my case, I was replacing a text field with an editor plugin, and the editor had not loaded by the time I called find().

就我而言,我正在用编辑器插件替换文本字段,而在我调用 find() 时编辑器尚未加载。

If you can't find the element when you know you should be able to, try setTimeout to add a delay before calling find().

如果您在知道应该可以找到时找不到该元素,请尝试在调用 find() 之前使用 setTimeout 添加延迟。

Note: setTimeout is not a good solution for this issue, it just helps diagnose the problem. Try to come up with a way to wait until the element has loaded before you make your find() call.

注意:setTimeout 不是解决这个问题的好方法,它只是帮助诊断问题。尝试想出一种方法,在您进行 find() 调用之前等待元素加载完毕。