html - 在手风琴标题中插入指向内部页面的链接
问题描述
我希望附加代码的所有功能保持原样,但是我希望每个手风琴的标题在打开手风琴下拉菜单时成为另一个页面的链接。每次我尝试时,我得到的只是没有成功链接的手风琴,或者相反。
如果您需要更多信息,请告诉我。
TLDR:将href附加到标签“标签一”
我目前正在使用附加的代码来解决我的问题 https://jsfiddle.net/11wunqqz/4/
/* Acordeon styles */
.tab {
position: relative;
margin-bottom: 1px;
width: 100%;
color: #fff;
overflow: hidden;
}
input {
position: absolute;
opacity: 0;
z-index: -1;
}
label {
position: relative;
display: block;
padding: 0 0 0 1em;
background: #16a085;
font-weight: bold;
line-height: 3;
cursor: pointer;
}
.blue label {
background: #2980b9;
}
.tab-content {
max-height: 0;
overflow: hidden;
background: #1abc9c;
-webkit-transition: max-height .35s;
-o-transition: max-height .35s;
transition: max-height .35s;
}
.blue .tab-content {
background: #3498db;
}
.tab-content p {
margin: 1em;
}
/* :checked */
input:checked~.tab-content {
max-height: 10em;
}
/* Icon */
label::after {
position: absolute;
right: 0;
top: 0;
display: block;
width: 3em;
height: 3em;
line-height: 3;
text-align: center;
-webkit-transition: all .35s;
-o-transition: all .35s;
transition: all .35s;
}
input[type=checkbox]+label::after {
content: "+";
}
input[type=radio]+label::after {
content: "\25BC";
}
input[type=checkbox]:checked+label::after {
transform: rotate(315deg);
}
input[type=radio]:checked+label::after {
transform: rotateX(180deg);
}
<div class="wrapper">
<div class="tab blue">
<input id="tab-four" type="radio" name="tabs2">
<label for="tab-four">Label One</label>
<div class="tab-content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tenetur, architecto, explicabo perferendis nostrum, maxime impedit atque odit sunt pariatur illo obcaecati soluta molestias iure facere dolorum adipisci eum? Saepe, itaque.</p>
</div>
</div>
<div class="tab blue">
<input id="tab-five" type="radio" name="tabs2">
<label for="tab-five">Label Two</label>
<div class="tab-content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tenetur, architecto, explicabo perferendis nostrum, maxime impedit atque odit sunt pariatur illo obcaecati soluta molestias iure facere dolorum adipisci eum? Saepe, itaque.</p>
</div>
</div>
<div class="tab blue">
<input id="tab-six" type="radio" name="tabs2">
<label for="tab-six">Label Three</label>
<div class="tab-content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tenetur, architecto, explicabo perferendis nostrum, maxime impedit atque odit sunt pariatur illo obcaecati soluta molestias iure facere dolorum adipisci eum? Saepe, itaque.</p>
</div>
</div>
</div>
解决方案
TLDR:将href附加到标签“标签一”
编辑:我看到了你的评论:
插入 href 会超出手风琴功能
您没有指定是否要避免使用 JavaScript,但我的解决方案涉及到它。我的解决方案是在您的标签上添加一个点击事件。看看我的小提琴:https ://jsfiddle.net/j5L8rhtm/
我在页面完成加载和解析后运行 JavaScript 代码(这非常重要,因为我需要访问 DOM),这包括向所有具有属性click
的标签添加事件。label
data-href
它有点像a href
,但它使用 JavaScript添加事件并避免覆盖已绑定到该项目的事件。事实上,我已经使用该addEventListener
方法为click
事件添加了一个匿名函数。
然后我使用该window.open
功能打开一个新窗口。您也可以打开一个新选项卡。检查这里的论点。
有些人可能会说window.open
应该避免这种情况,但是如果您使用它在click
事件中打开一个窗口/选项卡,使用它还不错,因为它是用户触发的操作。它“抵抗”浏览器的弹出限制和“更难”的弹出阻止扩展(我有其中之一,我的小提琴代码仍然可以正常工作)。
我不知道您是否有任何 JavaScript 技能,但是使用我在那个小提琴中的代码,您可以编写并分配标签data-href
上的属性。label
原来的:
我刚刚编辑过:
<label for="tab-four">Label One</label>
至:
<label for="tab-four"><a href="https://www.google.com">Label One</a></label>
它只是工作。我将“标签一”包装到a
带有该href
属性的标签中。您可以在其中放置任何值,例如锚点(无需刷新即可跳转到页面的特定部分)。您还可以指定一个target
属性。请参阅此处了解更多信息。
推荐阅读
- c# - 检查是否选择了两个列索引
- c# - 更改我的新列表而不更改原始列表
- sql - 按日历月聚合日期数据
- typescript - 在 TypeScript 中使用什么方法代替多重继承?
- .htaccess - htaccess - 帮助强制使用 WWW 和 HTTPS。当前结果为 https://www.example.com/example.com
- xml - 使用来自另一个 XML 的值在 XML 中随机生成一个值
- javascript - 删除表格内容并使用 JavaScript 替换
- c# - 为什么我的代码会显示 System.FormatException:“输入字符串的格式不正确。” 当我将文本框留空并单击提交按钮时?
- java - Java - 如何从字符串中设置泛型类中的类名?
- javascript - 从两个数组返回唯一元素