css 动态添加的删除按钮在点击时不起作用

ryevplcw  于 2023-02-26  发布在  其他
关注(0)|答案(1)|浏览(121)

这是我的简单代码,以获得用户在某个div搜索历史记录。这返回搜索历史记录内的div和这些搜索数据保存在本地存储。我已经添加了一个关闭按钮,当用户点击这个关闭按钮,这个特定的搜索数据应该从搜索历史记录以及本地存储中删除。但为什么这个关闭按钮不工作?
另一个事实是添加了这个关闭按钮后,它甚至不能从搜索历史中点击选择。为什么会发生这种情况?
下面是我的完整代码:

$(document).ready(function() {
  var $searchInput = $('#searchInput');
  var $searchBtn = $('#searchBtn');
  var $searchSuggestions = $('#searchSuggestions');
  var searchHistory = [];

  $searchInput.on('input', function() {
    var searchTerm = $searchInput.val().toLowerCase();
    var suggestions = getSuggestions(searchTerm);
    displaySuggestions(suggestions);
  });

  $searchInput.on('focus', function() {
    var searchTerm = $searchInput.val().toLowerCase();
    var suggestions = getSuggestions(searchTerm);
    displaySuggestions(suggestions);
  });

  $(document).on('click', function(event) {
    if (!$(event.target).closest('#searchInput, #searchSuggestions').length) {
      $searchSuggestions.hide();
    }
  });

  function getSuggestions(searchTerm) {
    var storedSearchHistory = JSON.parse(localStorage.getItem('searchHistory'));
    if (storedSearchHistory !== null) {
      searchHistory = storedSearchHistory;
    }
    var filteredSearchHistory = searchHistory.filter(function(item) {
      return item.toLowerCase().indexOf(searchTerm) !== -1;
    });
    return filteredSearchHistory;
  }

function displaySuggestions(suggestions) {
  $searchSuggestions.empty();
  if (suggestions.length > 0) {
    $searchSuggestions.show();
    for (var i = 0; i < suggestions.length; i++) {
      var suggestion = $('<div class="suggestion">' + suggestions[i] + '</div>');
      var removeButton = $('<button class="remove-btn">X</button>');
      suggestion.append(removeButton);
      $searchSuggestions.append(suggestion);
    }
  } else {
    $searchSuggestions.hide();
  }
}

  $(document).on('click', '.suggestion', function(event) {
    if ($(event.target).hasClass('removeBtn')) {
      var suggestion = $(event.target).prev().text();
      var suggestionIndex = searchHistory.indexOf(suggestion);
      if (suggestionIndex !== -1) {
        searchHistory.splice(suggestionIndex, 1);
        localStorage.setItem('searchHistory', JSON.stringify(searchHistory));
        displaySuggestions(getSuggestions($searchInput.val().toLowerCase()));
      }
    } else {
      var suggestion = $(this).text();
      $searchInput.val(suggestion);
      $searchSuggestions.hide();
    }
  });

  $searchBtn.on('click', function() {
    var searchTerm = $searchInput.val();
    if (searchTerm.trim() !== '') {
      searchHistory.push(searchTerm);
      localStorage.setItem('searchHistory', JSON.stringify(searchHistory));
      displaySuggestions([]);
    }
  });
});
#searchInput {
      padding: 5px;
      border: 1px solid #ccc;
      border-radius: 5px;
      width: 250px;
    }

    #searchBtn {
      padding: 5px;
      background-color: #4CAF50;
      color: white;
      border: none;
      border-radius: 5px;
      cursor: pointer;
    }

    #searchBtn:hover {
      background-color: #3E8E41;
    }

    #searchSuggestions {
      position: absolute;
      width: 250px;
      background-color: #fff;
      border: 1px solid #ccc;
      border-radius: 5px;
      display: none;
      margin-top: 10px;
      padding: 6px;
    }

    .suggestion {
      padding: 5px;
      margin: 5px;
      background: #f3f3f3;
      cursor: pointer;
      border-radius: 5px;
      display: inline-block;
    }
    .suggestion:hover {
      background-color: #eee;
    }
    .remove-btn{
    border: none;
    margin-left: 5px;
    }
<input type="text" id="searchInput" autocomplete="off" placeholder="Type your search term here...">
        <button id="searchBtn">Search</button>
        <div id="searchSuggestions"></div>
to94eoyn

to94eoyn1#

两件事:
1.您需要修复按钮类标识符:

if ($(event.target).hasClass('remove-btn')) {

1.修复字符串选择器,使其不包含子文本,现在它包含按钮中的x,因此indexOf总是失败:

var suggestion = $(event.target).parent().contents().filter(function() {return this.nodeType == 3; }).text();
    • 编辑**

任何单击都会隐藏列表,这就是为什么在删除时隐藏列表。要使列表在删除时保持打开状态,请防止在单击此处时隐藏列表,例如仅在未单击remove-btn时隐藏列表:

if (!$(event.target).closest('#searchInput, #searchSuggestions').length) {
  if(!$(event.target).hasClass('remove-btn')) $searchSuggestions.hide();
}

试试这个:
一个一个三个一个一个一个一个一个四个一个一个一个一个一个五个一个

相关问题