<div class="card-casino-container"> <div class="card-casino-logo"> <img…
- User
<!-- Кнопка для показа дополнительной информации --> <div class="card-casino-more-info-button"> <button onclick="showMoreInfo()"> <img src="images/btn down.png" alt="Иконка вниз" width="12" height="12"> <span>Подробнее</span> <img src="images/btn down.png" alt="Иконка вниз" width="12" height="12"> </button> </div> <!-- Контейнер с дополнительной информацией (по умолчанию скрыт) --> <div class="card-casino-more-info" id="moreInfoContainer"> <div class="card-casino-review-text"> <!-- Здесь добавьте текст обзора --> <p> Текст обзора казино GGBET. Здесь можно подробно описать особенности казино, бонусные предложения, игровые возможности и другие важные детали. </p> </div> <!-- Кнопка "Перейти на сайт" по центру --> <div class="card-casino-go-to-site-button"> <button onclick="goToSite()">Перейти на сайт</button> </div> <!-- Кнопка для скрытия дополнительной информации --> <div class="card-casino-hide-info-button"> <button onclick="hideMoreInfo()"> <img src="images/btn up.png" alt="Иконка вниз" width="12" height="12"> <span>Скрыть</span> <img src="images/btn up.png" alt="Иконка вниз" width="12" height="12"> </button> </button> </div> </div> </div> <!-- Карточка Казино 1 --> <div class="card-casino-container"> <div class="card-casino-logo"> <img src="images/Cosmobet.png" alt="Cosmobet Logo"> </div> <div class="card-casino-offer"> <div class="card-casino-offer-header"> <img src="images/ua-flag.png" alt="Flag of Ukraine"> <span>COSMOBET</span> <div class="stars-wrapper" bis_skin_checked="1"> <span class="icon-star"></span> <span class="icon-star"></span> <span class="icon-star"></span> <span class="icon-star"></span> <span class="icon-star half"></span> <div class="point" bis_skin_checked="1">4.8</div> </div> </div> <div class="card-casino-offer-details" data-i18n="offer_details1" > Вітальний бонус до 300 000₴ + 750 фріспінів </div> <!-- <div class="card-casino-category"> <span data-i18n="mindep">Мінімальний депозит</span> <span class="deposit-amount">100 UAH</span> </div>--> </div> <div class="card-casino-action-button" > <button data-i18n="casinoaction_button">Отримати</button> </div> <!-- Кнопка для показа дополнительной информации --> <div class="card-casino-more-info-button"> <button onclick="showMoreInfo()"> <img src="images/btn down.png" alt="Иконка вниз" width="12" height="12"> <span>Подробнее</span> <img src="images/btn down.png" alt="Иконка вниз" width="12" height="12"> </button> </div>// Обработчик клика на кнопку "Подробнее"
function showMoreInfo() { // Показываем контейнер с дополнительной информацией document.getElementById('moreInfoContainer').style.display = 'block'; // Скрываем кнопку "Дополнительная информация" document.querySelector('.card-casino-more-info-button').style.display = 'none'; }
function hideMoreInfo() { // Скрываем контейнер с дополнительной информацией document.getElementById('moreInfoContainer').style.display = 'none'; // Показываем кнопку "Дополнительная информация" document.querySelector('.card-casino-more-info-button').style.display = 'block'; }
function goToSite() { // Открываем сайт в новой вкладке window.open('https://ggbet.com', '_blank'); }
почему когда нажимаю на кнопку "Подробнее" в разширяется не та карточка
- Assistant
The issue you're experiencing is due to the way your JavaScript functions
showMoreInfo()andhideMoreInfo()are interacting with the DOM elements. Specifically, they're not targeting the specific card you intend to expand or collapse; instead, they operate on the first elements they find with the specified selectors or IDs. This is because:-
Duplicate IDs: In your HTML, you have multiple elements with the same
id="moreInfoContainer". In HTML, IDs must be unique within a page. If you have multiple elements with the same ID,document.getElementById('moreInfoContainer')will always select the first one, causing the wrong card to expand. -
Global Selectors: You're using
document.querySelector()without specifying the context, which defaults to searching the entire document. This means operations intended for one card might affect another.
Solution:
To fix this, you need to modify your JavaScript functions to operate within the context of the card that's interacted with. Here's how you can do it:
-
Use Classes Instead of IDs: Replace the
id="moreInfoContainer"with a class, such asclass="card-casino-more-info", to prevent ID duplication. -
Pass the Clicked Element to the Function: Modify your
onclickhandlers to passthis(the clicked button) to your functions. -
Modify Functions to Operate Within the Card: In your functions, use
closest()to find the nearest ancestor with the classcard-casino-container, ensuring actions are confined to the specific card. -
Remove Duplicate IDs: Ensure all IDs are unique or switch to using classes and relative selectors to target elements.
Below is the corrected code:
HTML:
<!-- Card 1 --> <div class="card-casino-container"> <!-- ... other content ... --> <div class="card-casino-more-info-button"> <button onclick="showMoreInfo(this)"> <img src="images/btn down.png" alt="Иконка вниз" width="12" height="12"> <span>Подробнее</span> <img src="images/btn down.png" alt="Иконка вниз" width="12" height="12"> </button> </div> <div class="card-casino-more-info" style="display: none;"> <!-- Additional info content --> <div class="card-casino-review-text"> <p> Текст обзора казино GGBET. Здесь можно подробно описать особенности казино, бонусные предложения, игровые возможности и другие важные детали. </p> </div> <div class="card-casino-go-to-site-button"> <button onclick="goToSite()">Перейти на сайт</button> </div> <div class="card-casino-hide-info-button"> <button onclick="hideMoreInfo(this)"> <img src="images/btn up.png" alt="Иконка вверх" width="12" height="12"> <span>Скрыть</span> <img src="images/btn up.png" alt="Иконка вверх" width="12" height="12"> </button> </div> </div> </div> <!-- Card 2 --> <div class="card-casino-container"> <!-- ... other content ... --> <div class="card-casino-more-info-button"> <button onclick="showMoreInfo(this)"> <img src="images/btn down.png" alt="Иконка вниз" width="12" height="12"> <span>Подробнее</span> <img src="images/btn down.png" alt="Иконка вниз" width="12" height="12"> </button> </div> <div class="card-casino-more-info" style="display: none;"> <!-- Additional info content for Card 2 --> <div class="card-casino-review-text"> <p> Текст обзора казино COSMOBET. Здесь можно подробно описать особенности казино, бонусные предложения, игровые возможности и другие важные детали. </p> </div> <div class="card-casino-go-to-site-button"> <button onclick="goToSite()">Перейти на сайт</button> </div> <div class="card-casino-hide-info-button"> <button onclick="hideMoreInfo(this)"> <img src="images/btn up.png" alt="Иконка вверх" width="12" height="12"> <span>Скрыть</span> <img src="images/btn up.png" alt="Иконка вверх" width="12" height="12"> </button> </div> </div> </div>JavaScript:
function showMoreInfo(button) { // Find the closest card container var cardContainer = button.closest('.card-casino-container'); // Show the more info container within the card var moreInfoContainer = cardContainer.querySelector('.card-casino-more-info'); moreInfoContainer.style.display = 'block'; // Hide the "Подробнее" button within the card var moreInfoButton = cardContainer.querySelector('.card-casino-more-info-button'); moreInfoButton.style.display = 'none'; } function hideMoreInfo(button) { var cardContainer = button.closest('.card-casino-container'); var moreInfoContainer = cardContainer.querySelector('.card-casino-more-info'); moreInfoContainer.style.display = 'none'; var moreInfoButton = cardContainer.querySelector('.card-casino-more-info-button'); moreInfoButton.style.display = 'block'; } function goToSite() { // Open the site in a new tab window.open('https://ggbet.com', '_blank'); }Explanation:
-
Passing
thisto Functions: By passingthisto your functions, you provide a reference to the button that was clicked. This allows the function to locate elements relative to that button. -
Using
closest(): Theclosest()method starts with the current element (in this case, the button) and traverses up the DOM tree until it finds an ancestor that matches the selector (the card container). -
Querying Within the Card: Once you have the card container, you can use
querySelector()to find elements within that specific card, rather than the entire document. -
Avoiding Duplicate IDs: By removing the duplicate IDs and using classes instead, you prevent conflicts and ensure that your selectors target the correct elements.
Good Practices:
-
Unique IDs: Always ensure that IDs are unique within the page.
-
Relative DOM Traversal: When dealing with multiple similar components, it's best to traverse the DOM relative to the clicked element to ensure actions are confined to the intended component.
-
Avoid Global Selectors: Using global selectors like
document.querySelector()without context can lead to unintended elements being selected. Always scope your selectors as narrowly as possible.
Summary:
By adjusting your functions to operate within the context of the clicked card, you ensure that the "Подробнее" and "Скрыть" buttons control the appropriate sections. This makes your code more scalable and avoids interference between multiple cards on the same page.
-