Javascript 悬停内容显示

发布于 2024-09-30 02:05:43 字数 1845 浏览 4 评论 0原文

我正在尝试编写一个 JS 函数(使用 Rails 中的原型),当鼠标悬停在 li 上时,它将显示 li 中隐藏的 div。每个 li 都有一个唯一的 id,它是一个数字,如下所示:

<li id="100">
  <div style="display:none;" id="hover-display-content">Content</div>
  <div style="display:none;" id="more-hover-display-content">Content</div>
  <div style="display:none;" id="even-more-hover-display-content">Content</div>
</li>

但我不确定如何执行此操作,特别是在 JS 只显示该特定 li 的隐藏元素的情况下。

我在想:

Event.observe(window, 'load', function() {  
  Event.observe($("li"), 'mouseover', function() {
     var id = readAttribute("id")
     id.getElementById("hover-display-content").style.display = "inline"; 
     id.getElementById("more-hover-display-content").style.display = "inline";
     id.getElementById("even-hover-display-content").style.display = "inline";
  });  
Event.observe($("li"), 'mouseout', function() {  
     var id = readAttribute("id")
     id.getElementById("hover-display-content").style.display = "none"; 
     id.getElementById("more-hover-display-content").style.display = "none";
     id.getElementById("even-hover-display-content").style.display = "none";
});  
});  

但这似乎不起作用。我哪里错了?

编辑:

我现在正在使用:

    Event.observe(window, 'load', function() {  
$$('li').invoke('observe', 'mouseover', function(event) {
    this.children[0].toggle();
});

$$('li').invoke('observe', 'mouseout', function(event) {
    document.children[0].toggle();
});
    });   

这是部分工作,但是我的代码如下所示:

<ul>
  <li>
    <div style="display:hidden;">Hidden Div</div>
    <div>More content that isn't hidden</div>
  </li>
</ul>

当我滚动 li 时,它会显示隐藏的 div,但是如果我滚动第二个 div,它会再次隐藏评论,即使这个 div 位于 li 中。为什么?

I am attempting to write a JS function (using prototype in rails) that will show hidden divs within a li when that li is mouseover'ed. Each li has a unique id that is a number, like so:

<li id="100">
  <div style="display:none;" id="hover-display-content">Content</div>
  <div style="display:none;" id="more-hover-display-content">Content</div>
  <div style="display:none;" id="even-more-hover-display-content">Content</div>
</li>

I'm not sure how to go about doing this though, especially where the JS only shows the hidden elemenst for that specific li.

I'm thinking something like:

Event.observe(window, 'load', function() {  
  Event.observe($("li"), 'mouseover', function() {
     var id = readAttribute("id")
     id.getElementById("hover-display-content").style.display = "inline"; 
     id.getElementById("more-hover-display-content").style.display = "inline";
     id.getElementById("even-hover-display-content").style.display = "inline";
  });  
Event.observe($("li"), 'mouseout', function() {  
     var id = readAttribute("id")
     id.getElementById("hover-display-content").style.display = "none"; 
     id.getElementById("more-hover-display-content").style.display = "none";
     id.getElementById("even-hover-display-content").style.display = "none";
});  
});  

But it doesn't seem to be working. Where am I going wrong?

Edit:

I am now using:

    Event.observe(window, 'load', function() {  
$('li').invoke('observe', 'mouseover', function(event) {
    this.children[0].toggle();
});

$('li').invoke('observe', 'mouseout', function(event) {
    document.children[0].toggle();
});
    });   

Which is partially working, however my code looks like the following:

<ul>
  <li>
    <div style="display:hidden;">Hidden Div</div>
    <div>More content that isn't hidden</div>
  </li>
</ul>

When I rollover the li it displays the hidden div, however if I rollover the second div it hides the comment again, even though this div is in the li. Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

灯角 2024-10-07 02:05:43

带有标签并悬停?只是提供一个想法

<html>
<head>
<style>
a div{display:none; height:10px;}
a:hover div{display:inline;}
</style>
</head>
<body>
<ul>
  <li><a>a<div id="hover-display-content">Content</div></a></li>
  <li><a>s<div id="more-hover-display-content">Content1</div></a></li>
  <li><a>d<div id="even-more-hover-display-content">Content2</div></a></li>
</ul>  
</body>
</html>

with a tag and hover? just providing an idea

<html>
<head>
<style>
a div{display:none; height:10px;}
a:hover div{display:inline;}
</style>
</head>
<body>
<ul>
  <li><a>a<div id="hover-display-content">Content</div></a></li>
  <li><a>s<div id="more-hover-display-content">Content1</div></a></li>
  <li><a>d<div id="even-more-hover-display-content">Content2</div></a></li>
</ul>  
</body>
</html>
箜明 2024-10-07 02:05:43

我不确定这是否是您唯一的问题,但一个关键问题是 DOM id 不能以数字开头,您需要在其前面添加诸如 model_name-100 之类的前缀。此外,所有 id 都必须是唯一的。因此,您的内部内容 div 需要将这些 id 转换为类似 class="even-more-hover-display-content"... 或者您可以在 id 前面加上父元素的 id 前缀,例如 model_name-100-even-more-hover-display-content

Im not sure if this is your only issue but one key thing is that DOM id's cannot begin with a number, youll need to prefix that with something like model_name-100. Additionally, all id's need to be unique. So your inner content div's need to have those id's converted to classes like class="even-more-hover-display-content"... or alternatively you could jsut prefix the id with the id of the parent element like model_name-100-even-more-hover-display-content.

我将从 .getElementByClass() 开始 --- 在 Proto 1.6 中已弃用

链接此处。

作为后备计划,以下是常规 JS 中有效的概念验证:

<li onmouseover="this.children[0].style.display = 'inline';
                 this.children[1].style.display = 'inline';"> 
   <div id="testdiv" style="display:none;background:blue">test</div>
   <div id="testdiv" style="display:none;background:blue">test</div>
</li>

I would start with the .getElementByClass() --- deprecated in Proto 1.6

Link here.

As a fallback plan, here's a proof-of-concept in regular JS that works:

<li onmouseover="this.children[0].style.display = 'inline';
                 this.children[1].style.display = 'inline';"> 
   <div id="testdiv" style="display:none;background:blue">test</div>
   <div id="testdiv" style="display:none;background:blue">test</div>
</li>
少钕鈤記 2024-10-07 02:05:43

使用 jquery

$('#id').each(function(){
$(this).css({diplay:'inline'});});

并不是那么简单。 。

using jquery

$('#id').each(function(){
$(this).css({diplay:'inline'});});

isn't that simple . .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文