简体   繁体   English

当特定元素具有相同的类名时,如何获取jQuery函数以使其与特定元素一起使用?

[英]How do I get a jQuery function to work with specific elements when they have the same class names?

I have sections (divs) with text in it, but when the text is too long I made it so the text "fades" (with css) and displays a "show more" button, which shows the full text for that specific div when clicked. 我在其中包含文本的部分(divs),但是当文本太长时,我将其设置为文本“ fades”(带有CSS)并显示“显示更多”按钮,该按钮会在该特定div时显示全文点击。 The problem is that it only works for the first div, and I believe it's because they all have the same class and id name. 问题在于它仅适用于第一个div,我相信这是因为它们都具有相同的类和id名称。 What's the best way to get around that? 解决这个问题的最佳方法是什么? Here's my code: 这是我的代码:

HTML: HTML:

<div id="fade-container">
  <div id="fade-content">
   <p>
    Long text goes here...
    <div class="fade-anchor"><span class="btn-primary round-xl small btn-shadow">Show more</span></div>
   </p>
  </div>
</div>

Script: 脚本:

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

By the way, info is being fetched from the database in a php while loop. 顺便说一下,信息是通过php while循环从数据库中获取的。

When the user clicks on .fade-anchor you can use this to get the element currently selected, you should also use classes instead of ids for multiple elements, like so: 当用户单击.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.
});

You can also check out this jsFiddle with the working version. 您也可以在工作版本中检出此jsFiddle

Hope it helps. 希望能帮助到你。

You can achieve it by e.currentTarget

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何仅访问具有相同 class 的特定元素? jquery - How do I access only specific elements that have a same class? jquery 当元素具有相同的类时,如何分配数据? - How do I assign data when elements have the same class? 当存在多个具有相同类名和属性名的元素时,获取该元素的特定文本值 - Get a specific text value of an element when there is multiple elements with the same class names and attribute names 如何获取没有特定名称的参数 - How do get params that have no specific names 在jQuery中,当它们都具有相同的名称时,如何获得单选按钮的值? - In jQuery, how do I get the value of a radio button when they all have the same name? 如何将2个或多个jquery函数与功能相同但名称不同的元素组合在一起 - How can I combine 2 or more jquery functions with elements that do the same thing but with different names 如何使用相同的数据填充页面上的多个元素(具有相同的类)? - How do I populate several elements on a page (that have the same class) with the same data? 如何获取类的属性名称 - How do I get the property names of a class jQuery函数循环遍历元素并根据顺序设置类名称,与具有特定类名称的元素相比 - jQuery function to loop through elements and set class names based on order compared to an element with a specific class name 我没有动态创建的元素相同的渲染 - JQuery - Bootstrap - I do not have the same rendering with elements created dynamically - JQuery - Bootstrap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM