jquery - 使用 jquery 中的目标在隐藏的图像和文本之间切换
问题描述
我有 4 个项目的页面。它们是苹果、香蕉、橙子和梨。我想在列表中单击他们的标题,以便分别切换他们的图像和文本。例如:当我点击列表中的“apple”时,图像属于.imagebox中的#imageapple,id为#textapple的文本应该只显示,窗口中的url得到一个新的hash#apple。当我单击标题橙色时,它只显示#imageorange 和#textorange,并且窗口中的url 获取哈希#orange。我正在尝试 target,attr 而不是在 Jquery 中,但我的代码不起作用。
这是代码:
$(function(){
$('.title').click(function(){
$('.imagebox').not('#image' + $(this).attr('target')).hide();
$('#image' + $(this).attr('target')).toggle();
$('.text').not('#text' + $(this).attr('target')).hide();
$('#text' + $(this).attr('target')).toggle();
window.location.hash = target;
});
});
nav{
width: 10%;
index:999;
padding-left: 1vh;
flex:1;
flex-basis: 18%;
overflow: none;
}
nav > ul.menu {
list-style-type: none;
}
li{
color:black;
font-family: 'Rubik', sans-serif;
font-size: 2vh;
}
li:hover {
font-style: italic;
background-color: black;
color: white;
cursor: pointer;
}
.content{
flex:1;
flex-basis: 82%;
display: flex;
}
.imagebox{
flex: 1;
overflow: scroll;
width: 70%;
height: 100vh;
left:0;
overflow: -moz-scrollbars-none;
-ms-overflow-style: none;
}
.imagebox img{
width: 100%;
}
.text {
height: 100vh;
overflow: scroll;
margin-right: 2vh;
margin-left:2vh;
width: 30%;
-ms-overflow-style: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Kexin Hao</title>
</head>
<body>
<nav>
<ul class="menu">
<li class="title" target="apple">APPLE</li>
<li class="title" target="banana">BANANA</li>
<li class="title" target="orange"> ORANGE</li>
<li class="title" target="pear">PEAR</li>
</ul>
</nav>
解决方案
不确定我是否理解 100%,但我有一些可能会有所帮助的东西。
首先,您所拥有的存在一些问题。在 HTML 中,您需要定义一个 imagebox 元素,其中将显示图像(如果我错了,请纠正我)并且我已经添加了这个。尝试重定向到图像文件源的行也出现崩溃window.location.hash = target;
,但需要解析目标元素(参见附加代码)。
我使图像选择代码更简单一些,现在当单击链接时,目标被解析(Apple/banana/orange/pear),图像被选择并添加到图像框中。文本的想法是相同的。
这是我使用的 HTML / JQuery(图像名称在您的系统上可能略有不同)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css.css">
<title>Kexin Hao</title>
<script>
$(function(){
$('.title').click(function()
{
var ClickedTarget = $(this).attr('target');
$(".imagebox").attr("src",ClickedTarget+".jpg");
});
});
</script>
</head>
<body>
<nav>
<ul class="menu">
<li class="title" target="apple">APPLE</li>
<li class="title" target="banana">BANANA</li>
<li class="title" target="orange"> ORANGE</li>
<li class="title" target="pear">PEAR</li>
</ul>
</nav>
<img class ='imagebox'> </img>
</body>
</html>
希望这可以帮助!
推荐阅读
- javascript - 如何在 PHP 中翻转字节数组
- django - NGINX docker-compose - 在上游 nuxt:3000 中找不到主机
- android - Android Studio 上未将手机检测为 Android 设备
- node.js - AZURE 不允许使用 HTTP 错误 405 方法
- android - 在 Android Studio 中 Flutter 运行失败原因:java.lang.IllegalStateException:缺少 Crashlytics 构建 ID
- javascript - 在 html 网页上显示列表 (JSON)
- python - 错误 FileNotFoundError [winError 2] anaconda
- azure - Azure CLI 无法连接到 Docker 守护程序
- java - java:通过套接字发送大文件
- flutter - Flutter - 在侦听器内多次显示警报对话框