HTML onClick 用于字典模式实现


html onclick for dictionary mode implementation

这里的HTML新手..

我正在尝试实现一个网页,该网页弹出一些选定的技术单词的含义,如下所示。我有一个以csv格式提供的单词列表及其含义。如何自动从列表中选择可用单词列表的含义?

<!DOCTYPE html>
<html>
<head>
<style>
.dropbtn {
}
.dropbtn:hover, .dropbtn:focus {
    background-color: #3e8e41;
}
.dropdown {
    position: relative;
    display: inline-block;
}
.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    overflow: auto;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.show {display:block;}
</style>
</head>
<body>
<h2></h2>
<p>Hands-On with <div class="dropdown">
<button id="myBtn" class="dropbtn">Ultrahaptics'</button>
  <div id="myDropdown" class="dropdown-content">
    <a href="#home">Feel of touch without wearing devices</a>
  </div>
</div>
 gave us a feel of technology of future.
</p>
<script>
// Get the button, and when the user clicks on it, execute myFunction
document.getElementById("myBtn").onclick = function() {myFunction()};
/* myFunction toggles between adding and removing the show class, which is used to hide and show the dropdown content */
function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {
    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
</script>
</body>
</html>

PS:像这样的事情

<a title="Feel of touch without wearing devices">Ultrahaptics'</a> 

仅适用于鼠标指针,不适用于触摸设备。因此,寻找一种也适用于触摸设备的解决方案。

我可以看到你使用javascript。我建议使用jQuery-CSV

这将帮助您使用JavaScript读取CSV。您可以访问此链接,因为这与您的问题有些相关。埃文斯回答

使用 jQuery,这将是一个简单的情况:

$('.dropbtn').click(function(){
    $(this).siblings('.dropdown-content').toggleClass('hide');
});

使用以下 CSS:

.dropdown-content{
  position:absolute;
  background:#000;
  color:#fff;
}
.hide{
  display:none;
}

我已经简化了你的 HTML:

<p>
  Hands-On with
  <span class="dropdown">
    <button type="button" class="dropbtn">Ultrahaptics'</button>
    <a href="#home" class="dropdown-content hide">Feel of touch without wearing devices</a>
  </span>
  gave us a feel of technology of future.
</p>

你可以在这里看到它的实际效果:https://jsfiddle.net/a73t8L4m/

您可能也对使用 dfn 标记感兴趣,但这会使您的 HTML 看起来略有不同。