搜索输入时不出现工具提示

问题描述 投票:0回答:1

我发现自己又在工具提示上苦苦挣扎了。我添加了一个输入来执行搜索,当我执行任何搜索并尝试查看工具提示时,我只看到容器(及其背景颜色、边框等),但看不到工具提示的内容。我将留下这个例子,以便您能够理解发生了什么:

document.getElementById('searchInput').addEventListener('keyup', function() {
            const searchText = this.value.toLowerCase();
        
            document.querySelectorAll('.table-mercado tbody tr').forEach(row => {
                const nameCell    = row.querySelector('td:nth-child(2)');
                const priceCell  = row.querySelector('td:nth-child(3)');
        
                const name = nameCell ? nameCell.textContent.toLowerCase() : '';
                const price = priceCell ? priceCell.textContent.toLowerCase() : '';
                
                if (name.includes(searchText) || price.includes(searchText)){
                    row.style.display = '';
                } else {
                    row.style.display = 'none';
                }
    
            });
        });
.table-mercado {
        border-collapse: collapse;
        width: 100%;
        border-spacing: 0; 
        border: none; 
    }

    .table-mercado .th-mercado, .td-mercado, .tr-mercado {
        padding: 10px;
        text-align: center;
    }

    .td-mercado div, .td-mercado form {
        justify-content: center;
        margin: auto;
    }

    .td-mercado button {
        margin: 0 auto; 
        display: block; 
    }

    .tooltip-mercado .tooltiptext-mercado {
        display: block;
        visibility: hidden;
        opacity: 0;
        width: 250px; 
        background-color: black;
        color: #fff;
        text-align: left;
        padding: 15px;
        border-radius: 5px;
        position: absolute;
        left: 100%; 
        top: 50%;
        transform: translateY(-50%); 
        z-index: 9999; 
        opacity: 0;
        transition: opacity 0.3s, visibility 0.3s;
        margin-left: 10px;
        box-sizing: border-box;
        white-space: nowrap;
        border: 1px solid #ddd;
        
    }

    .tooltiptext-mercado table {
        border-collapse: collapse;
        width: 100%;
    }

    .tooltiptext-mercado form {
        width: 100%;
        margin-top: 10px;
    }

    .tooltip-mercado .tooltiptext-mercado::after {
        content: "";
        position: absolute;
        top: 50%;
        left: -10px;
        transform: translateY(-50%);
        border-width: 5px;
        border-style: solid;
        border-color: transparent black transparent transparent;
    }

    .block-img:hover .tooltiptext-mercado{
        visibility: visible !important;  
        opacity: 1 !important; 
    }
    
    .th-mercado {
        cursor: pointer;
        position: relative;
        color: black;
        padding-right: 20px;
        text-align: left;
    }

    .th-mercado::after {
        content: '';
        position: absolute;
        right: 5px;
        top: 50%;
        transform: translateY(-50%);
        font-size: 12px;
        opacity: 0.8;
        color: white;
    }
  
    
<input type="text" id="searchInput">
<div class="table-container">
    <table class="table-mercado">
        <thead>
            <tr class="tr-mercado">
                <th class="th-mercado"></th>
                <th class="th-mercado" >Nombre</th>
                <th class="th-mercado" >Price</th>
            </tr>
        </thead>
        <tbody>
                <tr class="row-mercado">
                    <td class="td-mercado" style="width: 60px; height: 60px; text-align: center; position: relative;">
                        <div class="block-img">
                            <img src="https://img.freepik.com/free-vector/earth-globe-icon-white-background_1308-125016.jpg" style="width: 60px; height: 60px; display: block;">   
                            <!-- Tooltip -->
                            <div class="tooltip-mercado">
                                <span class="tooltiptext-mercado">
                                    <table>
                                        <tr class="trtooltipmercado">
                                                <td>content</td>
                                        </tr>
                                    </table>
                                </span>
                            </div>
                        </div>
                    </td>
                    <td class="td-mercado"> Name1 </td>
                    <td class="td-mercado"> 10 </td>
                </tr>
                <tr class="row-mercado">
                    <td class="td-mercado" style="width: 60px; height: 60px; text-align: center; position: relative;">
                        <div class="block-img">
                            <img src="https://img.freepik.com/free-vector/earth-globe-icon-white-background_1308-125016.jpg" style="width: 60px; height: 60px; display: block;">   
                            <!-- Tooltip -->
                            <div class="tooltip-mercado">
                                <span class="tooltiptext-mercado">
                                    <table>
                                        <tr class="trtooltipmercado">
                                                <td>content</td>
                                        </tr>
                                    </table>
                                </span>
                            </div>
                        </div>
                    </td>
                    <td class="td-mercado"> Name2 </td>
                    <td class="td-mercado"> 50 </td>
                </tr>                                
        </tbody>
    </table>
</div>

javascript html tooltip
1个回答
0
投票

在此过程中您也会切换工具提示内容。您应该限制查询选择器。 要修复它,请将其更改为

.table-mercado > body > tr
。所以应该是:

document.getElementById('searchInput').addEventListener('keyup', function () {
    const searchText = this.value.toLowerCase();

    document.querySelectorAll('.table-mercado tbody tr').forEach(row => {
        const nameCell = row.querySelector('td:nth-child(2)');
        const priceCell = row.querySelector('td:nth-child(3)');

        const name = nameCell ? nameCell.textContent.toLowerCase() : '';
        const price = priceCell ? priceCell.textContent.toLowerCase() : '';

        if (name.includes(searchText) || price.includes(searchText)) {
            row.style.display = '';
        } else {
            row.style.display = 'none';
        }

    });
});

document.getElementById('searchInput').addEventListener('keyup', function() {
  const searchText = this.value.toLowerCase();

  document.querySelectorAll('.table-mercado > tbody > tr').forEach(row => {
    const nameCell = row.querySelector('td:nth-child(2)');
    const priceCell = row.querySelector('td:nth-child(3)');

    const name = nameCell ? nameCell.textContent.toLowerCase() : '';
    const price = priceCell ? priceCell.textContent.toLowerCase() : '';

    if (name.includes(searchText) || price.includes(searchText)) {
      row.style.display = '';
    } else {
      row.style.display = 'none';
    }

  });
});
.table-mercado {
  border-collapse: collapse;
  width: 100%;
  border-spacing: 0;
  border: none;
}

.table-mercado .th-mercado,
.td-mercado,
.tr-mercado {
  padding: 10px;
  text-align: center;
}

.td-mercado div,
.td-mercado form {
  justify-content: center;
  margin: auto;
}

.td-mercado button {
  margin: 0 auto;
  display: block;
}

.tooltip-mercado .tooltiptext-mercado {
  display: block;
  visibility: hidden;
  opacity: 0;
  width: 250px;
  background-color: black;
  color: #fff;
  text-align: left;
  padding: 15px;
  border-radius: 5px;
  position: absolute;
  left: 100%;
  top: 50%;
  transform: translateY(-50%);
  z-index: 9999;
  opacity: 0;
  transition: opacity 0.3s, visibility 0.3s;
  margin-left: 10px;
  box-sizing: border-box;
  white-space: nowrap;
  border: 1px solid #ddd;
}

.tooltiptext-mercado table {
  border-collapse: collapse;
  width: 100%;
}

.tooltiptext-mercado form {
  width: 100%;
  margin-top: 10px;
}

.tooltip-mercado .tooltiptext-mercado::after {
  content: "";
  position: absolute;
  top: 50%;
  left: -10px;
  transform: translateY(-50%);
  border-width: 5px;
  border-style: solid;
  border-color: transparent black transparent transparent;
}

.block-img:hover .tooltiptext-mercado {
  visibility: visible !important;
  opacity: 1 !important;
}

.th-mercado {
  cursor: pointer;
  position: relative;
  color: black;
  padding-right: 20px;
  text-align: left;
}

.th-mercado::after {
  content: '';
  position: absolute;
  right: 5px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 12px;
  opacity: 0.8;
  color: white;
}
<input type="text" id="searchInput">
<div class="table-container">
  <table class="table-mercado">
    <thead>
      <tr class="tr-mercado">
        <th class="th-mercado"></th>
        <th class="th-mercado">Nombre</th>
        <th class="th-mercado">Price</th>
      </tr>
    </thead>
    <tbody>
      <tr class="row-mercado">
        <td class="td-mercado" style="width: 60px; height: 60px; text-align: center; position: relative;">
          <div class="block-img">
            <img src="https://img.freepik.com/free-vector/earth-globe-icon-white-background_1308-125016.jpg" style="width: 60px; height: 60px; display: block;">
            <!-- Tooltip -->
            <div class="tooltip-mercado">
              <span class="tooltiptext-mercado">
                                    <table>
                                        <tr class="trtooltipmercado">
                                                <td>content</td>
                                        </tr>
                                    </table>
                                </span>
            </div>
          </div>
        </td>
        <td class="td-mercado"> Name1 </td>
        <td class="td-mercado"> 10 </td>
      </tr>
      <tr class="row-mercado">
        <td class="td-mercado" style="width: 60px; height: 60px; text-align: center; position: relative;">
          <div class="block-img">
            <img src="https://img.freepik.com/free-vector/earth-globe-icon-white-background_1308-125016.jpg" style="width: 60px; height: 60px; display: block;">
            <!-- Tooltip -->
            <div class="tooltip-mercado">
              <span class="tooltiptext-mercado">
                                    <table>
                                        <tr class="trtooltipmercado">
                                                <td>content</td>
                                        </tr>
                                    </table>
                                </span>
            </div>
          </div>
        </td>
        <td class="td-mercado"> Name2 </td>
        <td class="td-mercado"> 50 </td>
      </tr>
    </tbody>
  </table>
</div>

© www.soinside.com 2019 - 2024. All rights reserved.