【问题标题】:Nested <a> and <span> challenge嵌套 <a> 和 <span> 挑战
【发布时间】:2010-05-28 15:23:21
【问题描述】:

试图让嵌套链接在嵌套跨度内工作是徒劳的。 This 是下面代码的工作测试页面,用于解释我正在尝试做的事情。

关于如何让这个在有效的 html 中工作的任何想法?我想这要么是嵌套顺序,要么是样式语法,但我不知所措。任何想法都非常感谢。

<div id="greyback">
<ul id="scrollbox">
<li class="listcat">List header</li>
<li><a class="menu" href="#freeze">List item 1<span><b>This text has popped up because you have clicked the list item, which has an "a" tag and now has :focus. That "a" tag is the first of two.</b><br><br>What I am trying to do is to set the second "a" tag as a DIFFERENT "embedded" link in this box<span style="color: blue; background-color: yellow;">eg, here<a href="http://www.conservationenterprises.com" target="blank">This is the second (nested) "a" tag in this html nest.  It is a link to an external site. Instead of this being an always-visible link, I want it to sit within the yellow box in the first span (click the List item 1 to display).</a></span>
</a></span>
</li>
</a></span>
</li>
</ul>
</div>

和 CSS:

#scrollbox  {margin: 0 auto; margin-top: 1em;  margin-bottom: 1em; width:19em; height:auto;  max-height: 21em; overflow:auto; border-bottom: 0.1em solid #FFA500; border-top: 0.1em solid #FFA500;}

#scrollbox a {float: left; color:#000000; text-decoration:none; width:18em; height: auto; margin-bottom: 0.5em; font-family: Arial, sans-serif; font-size: 0.9em; text-align:left;}
#scrollbox a.menu {}
#scrollbox a span {display:none; position:absolute; left:0em; top:0;}
#scrollbox a span img {float: right; border:0; max-width:7.5em;}
#scrollbox a:hover {border: 0; color: #7ebb11; font-size:0.9em;}
#scrollbox a:hover span {border: 0; color: #535353;}
#scrollbox a span:focus {color: blue;}
#scrollbox a:active {border:none; color: #535353; text-decoration: none;}
#scrollbox a:focus {border:0em solid #000; outline:0;}
#scrollbox a:active span, #scrollbox li a:active span, #scrollbox a:focus span, #scrollbox li a:focus span  {display: block; width: 52.5em; min-height: 20em; height: auto; left: 1.5em; top:18em;  z-index:10; font-size:0.9em; text-align: left; padding: 1em; padding-bottom: 0em; background-color: #c3FFe3; color: #535353; border: solid #FFA500 0.25em;}
#scrollbox li a:active span span, #scrollbox li a:focus span span{display: block; width: auto; height: auto; min-height: 2em;  left: 25em; top:10em;  z-index:10; font-size:0.9em; text-align: left; padding: 1em; padding-bottom: 0em; background-color: transparent; color: #535353;  border: dashed red 1px;}

.ul#scrollbox {padding-left: 0.1em;}

#scrollbox li {float:left; list-style: none; background: url(blank.png) no-repeat left center; margin-left: 0em; font-family:Arial, sans-serif; font-size: 0.9em;}
#scrollbox li.listcat {float: left; text-align:left; width: 18em; margin-left: 0em; margin-top: 0.1em; margin-bottom: 0.3em; padding-top:0.5em; color: green; font-size: 0.9em; font-weight:bold;} 

干杯 帕特里克。

【问题讨论】:

  • 为什么不直接使用 javascript 来隐藏/显示多余的框?这样比尝试在 css 中实现所有这些要容易得多(或者您是否打算真的只使用纯 css 来解决这个问题?)
  • @corroded 我只希望使用 CSS,因为如果它咬我我将无法识别 javascript,但如果这就是它所需要的......
  • 这里有简单的 javascript 教程,还有一些工具可以帮助你,比如 jquery(如果你有时间,你可以检查一下)我可以尝试在一段时间内为你修复一个简单的示例

标签: html css css-selectors


【解决方案1】:

是的,你的结束标签放错了位置,我认为你的跨度放错了位置,但只是验证会显示这些错误。

  • @PaddyO - 通过它,你会注意到你正在这样做:这是非法的。
  • 谢谢,我明白了。会以正确的顺序试一试。干杯。
  • 感谢 Rob,修复了这些问题并设法获得了几乎所有的验证,除非因为我其中一个序列具有(并且如果我试图制作一个跨越的 则需要具有保持第二个 活动) W3C 不喜欢它(特别不喜欢 在另一个 . 现在学习一些 javascript...
  • @PaddyO 如果您的标记无效,学习 javascript 将无济于事。不要嵌套 A 元素,这对你来说会容易得多。
  • 【解决方案2】:

    如果您使用的是 jquery(或使用 $ 符号的类似 javascript 框架):

    <div id="popup" style="display: hidden">
       <span><b>This text has popped up because you have clicked the list item, which has an "a" tag and now has :focus. That "a" tag is the first of two.</b>
    </div>
    
    <li>
      <a href="your first link" onclick="$('popup').setStyle('display: block;')">first link</a>
      <a href="second link">second link</a>
    </li>
    

    您也可以使用 onfocus 代替 onclick。如果您没有 javascript 库,请使用 document.getElementById('popup') 而不是 $ 符号。

    这里有一个关于如何在 jquery 中隐藏一些 div 的非常简单的教程:http://api.jquery.com/hide/

    【讨论】:

    • 感谢 jquery 指针,我去看看。多学习一些总是好的,看起来这就是所需要的。非常感谢。
    • 不用担心。我自己更像是一个 xhtml/css 人,我不想用 10 英尺长的杆子接触 JS,但显然学习它也很好,尤其是使用框架(moo-tools、jquery、原型)。它非常有助于打造良好的 UI 和用户体验 :)
    【解决方案3】:

    您当然需要为此使用 Javascript(尽管肯定不是 Jquery,尤其是当 Javascript 对您来说已经很陌生时)。现在,您正尝试在另一个链接中嵌入一个链接,但我认为这无法正常工作。

    我建议使用唯一的div id,HTML DOM 会显示您的 div 和链接。这过于简单化了,但是是这样的:

    <div id="one"><a href="#" onclick="document.getElementById('two').style.display='block'">Link 1</a></div>
    <div id="two" style="display: none;">Here it is</div>
    

    W3Schools.com 有一个非常好的Javascript tutorial 可以帮助您入门。

    编辑:一些旁注。学习 Javascript 基础知识将非常值得您花时间。另外,尽量使用well-formed html

    【讨论】:

    • 谢谢,我给Javascript看看。出于兴趣(尽管我总是在可能的情况下更喜欢“合法”,但 Firefox 会在 www.conservationenterprises.com/portfolio.html 上完美地解释一段非法代码 -
        中的
        并执行我想做的事情. 但我会坚持下去,把它做好。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签