Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 正确定位图像src(给图像一个新的src)_Javascript_Html - Fatal编程技术网

Javascript 正确定位图像src(给图像一个新的src)

Javascript 正确定位图像src(给图像一个新的src),javascript,html,Javascript,Html,目前我正在尝试将单击时的图像更改为其他图像。我用以下代码成功地做到了这一点:(正在工作) 变量预选=”https://upload.wikimedia.org/wikipedia/commons/thumb/a/a2/X_mark.svg/525px-X_mark.svg.png"; 所选变量=”https://cdn.pixabay.com/photo/2016/03/31/14/37/check-mark-1292787_960_720.png"; var switchImage=函数(图

目前我正在尝试将单击时的图像更改为其他图像。我用以下代码成功地做到了这一点:(正在工作)


变量预选=”https://upload.wikimedia.org/wikipedia/commons/thumb/a/a2/X_mark.svg/525px-X_mark.svg.png";
所选变量=”https://cdn.pixabay.com/photo/2016/03/31/14/37/check-mark-1292787_960_720.png";
var switchImage=函数(图像){
if(image.src==预选){
image.src=已选择;
}否则{
image.src=预选;
}
};

这是可行的,但只有当我给imgsrc一个URL时。我想使用:src=“selectedimg.png”和src=“preselectedimg.png”,但是当我这样做时,它似乎找不到图像,因为没有切换图像。是的,图像在我的wwwroot文件夹中有这些名称。

如果此页面位于根目录的文件夹中,除非使用

例如

您还可以使用如下所示的
html标记重置当前页面的基本url

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>This is an example for the <base> element</title>
        <base href="http://www.example.com/news/index.html">
    </head>
    <body>
        <p>Visit the <a href="archives.html">archives</a>.</p>
    </body>
</html>

这是元素的一个示例
访问

编辑:


再看看

假设您有两张图片

<img src="image1.jpg" id="img1" />
<img src="image2.jpg" id="img2" />

根据我的理解,您想点击img2并更改img1。因此,您需要在img2标签中添加以下内容:

<img src="image2.jpg" id="img2" 
 onclick="document.getElementById('img1').src = 'image3.jpg'" />


就这样。我希望它能有所帮助:)

如果你想这样做,你的图像必须与调用它的文件路径相同。我需要的是:显示我的完整页面url/+selectedimg.png
<img src="image1.jpg" id="img1" />
<img src="image2.jpg" id="img2" />
<img src="image2.jpg" id="img2" 
 onclick="document.getElementById('img1').src = 'image3.jpg'" />