gpt4 book ai didi

javascript - HTML/JS 替换导航栏上的图像点击

转载 作者:太空宇宙 更新时间:2023-11-03 21:27:34 25 4
gpt4 key购买 nike

我想在每次点击导航栏元素时替换图片。

基本上,我正在尝试获得与该网站相同的导航栏行为:http://jjhale.com/

这是我到目前为止所做的:

HTML 文件:

<html lang="en">
<head>
<meta charset="utf-8" />
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<link rel="stylesheet" type="text/css" href="StyleSheet.css" />
<script src="scripts/jquery-1.11.3.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<!--JS Scripts-->
<script src="scripts/onClick.js"></script>

<title>Title</title>
</head>
<body>
<div id="page-container">
<div id="header">
<h1><a id="name" href="Link_1.html">Test</a></h1>
</div>
<div id="slides-zone">
<img id="image" src="images/1.jpg" alt="1" />
</div>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<ul class="nav navbar-nav">
<li class="selected"><a href="Link_1.html" onclick="testFunction();">Link 1</a></li>
<li><a href="Link_2.html">Link 2</a></li>
</ul>
</div>
</nav>
</div>

</body>
</html>

我的脚本应该在单击任何导航项时替换图像:

var testFunction = function () {

var element = document.getElementById('slides-zone');
var img_to_replace = document.getElementById('image');

var new_img = document.createElement("img");
new_img.setAttribute('src', 'images/2.jpeg');
new_img.setAttribute('alt', 'image_2');
new_img.setAttribute('id', 'image')
element.replaceChild(new_img, img_to_replace);

return true;
}

我希望仅在可能的情况下使用 Javascript 获得解决方案/任何信息,我不希望使用任何框架。

提前致谢

最佳答案

这是我的尝试

body {
background-color: #000;
}
img {
width: 800px;
height: 400px;
margin-left: auto;
margin-right: auto;
}
#slides-zone {
width: 800px;
height: 400px;
overflow: hidden;
}
<script>
function pictureChange() {
document.getElementById("theImage").src = "http://www.musicmatters.ie/images/volunteer2.jpg";
}

function pictureChange2() {
document.getElementById("theImage").src = "http://www.musicmatters.ie/images/bara4.jpg";
}
</script>

<body>
<div id="page-container">
<div id="header">
<h1><a id="name" href="#">Test</a></h1>
</div>
<div id="slides-zone">
<img id="theImage" src="http://www.musicmatters.ie/images/bara2.jpg" />
<img id="theImage" src="http://www.musicmatters.ie/images/bara4.jpg" />
</div>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<ul class="nav navbar-nav">
<li class="selected" onclick="pictureChange();"><a href="#">Link 1</a>
</li>
<li onclick="pictureChange2();"><a href="#">Link 2</a>
</li>
</ul>
</div>
</nav>
</div>

</body>

编辑:几行 jquery 就可以让您轻松切换图像

      $(function(){
$(".img-swap").live('click', function() {
if ($(this).attr("class") == "img-swap") {
this.src = this.src.replace("_off","_on");
} else {
this.src = this.src.replace("_on","_off");
}
$(this).toggleClass("on");
});
});

然后将文件命名为 xyz_off.jpg 和 xyz_on.jpg 等...

关于javascript - HTML/JS 替换导航栏上的图像点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31710993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com