Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当特定元素具有相同的类名时,如何让jQuery函数处理它们?_Javascript_Jquery_Css_Function - Fatal编程技术网

Javascript 当特定元素具有相同的类名时,如何让jQuery函数处理它们?

Javascript 当特定元素具有相同的类名时,如何让jQuery函数处理它们?,javascript,jquery,css,function,Javascript,Jquery,Css,Function,我有带文本的部分(div),但当文本太长时,我将其设置为文本“淡出”(使用css),并显示“显示更多”按钮,单击时显示该特定div的全文。问题是它只适用于第一个div,我相信这是因为它们都有相同的类和id名称。最好的办法是什么?这是我的密码: HTML: 这里有很长的文字。。。 显示更多 脚本: <script> $('.fade-anchor').click(function(e){ e.preventDefault(); $('#fade-content'

我有带文本的部分(div),但当文本太长时,我将其设置为文本“淡出”(使用css),并显示“显示更多”按钮,单击时显示该特定div的全文。问题是它只适用于第一个div,我相信这是因为它们都有相同的类和id名称。最好的办法是什么?这是我的密码:

HTML:



这里有很长的文字。。。
显示更多

脚本:

<script>
$('.fade-anchor').click(function(e){
    e.preventDefault();
    $('#fade-content').css('max-height','none');
    $('.fade-anchor').remove();
});
</script>

$('.fade anchor')。单击(函数(e){
e、 预防默认值();
$(“#淡入内容”).css('max-height','none');
$('.fade-anchor').remove();
});

顺便说一下,信息是在php while循环中从数据库中获取的。

当用户单击
时。fade anchor
您可以使用
this
来获取当前选定的元素,您还应该对多个元素使用类而不是ID,如下所示:

$('.fade-anchor').click(function(e){
    e.preventDefault();
    $(this).parent('.fade-content').css('max-height','none');
    $(this).hide(); // Maybe you should hide instead of removing, in case you want to add a toggle effect later on.
});
您也可以在工作版本中查看这一点


希望有帮助。

您不能有多个元素具有相同的id。只需使用类。
$(此)。最近('.fade content')。toggleClass('open')
是您的朋友,在将id更改为class^^谢谢。我还没有那么丰富的经验,但我现在记得关于身份证。谢谢你!非常感谢卢卡斯!这很有效。谢谢你花时间帮助像我这样的新手。
You can achieve it by e.currentTarget

$('.fade-anchor').click(function(e){
e.preventDefault();
$(e.currentTarget).css('max-height','none');
$('.fade-anchor').remove();});
You can achieve it by e.currentTarget

$('.fade-anchor').click(function(e){
e.preventDefault();
$(e.currentTarget).css('max-height','none');
$('.fade-anchor').remove();});