我正在尝试根据 #var=1`(或 2、或 3、或 4)中的变量更改 aspx asp.net c# 页面上图像的 imageurl
不幸的是,我对 javascript 一无所知,而这正是我被告知我需要的。谁能给我指出一个基于新手的脚本,我可以尝试通过实现来学习?
最佳答案
你的问题很模糊,所以我不确定你到底需要什么......我只是写一些代码,看看它是否接近目标
尝试类似的事情
<img id="someUniqueIdYouMakeUp">
<script type="text/javascript">
theImage = document.getElementById("someUniqueIdYouMakeUp");
if(window.location.hash == "#var=1")
{
theImage.src = "/some/image.jpg";
}
else if(window.location.hash == "#var=2")
{
theImage.src = "/some/other/image.jpg";
}
</script>
编辑:根据您的评论,您正在寻找要更新的图像,即使哈希值在页面加载后发生变化也是如此。为此,您需要类似于以下的代码:
var updateImageWhenHashChanges = function()
{
theImage = document.getElementById("someUniqueIdYouMakeUp");
if(window.location.hash == "#var=1")
{
theImage.src = "/some/image.jpg";
}
else if(window.location.hash == "#var=2")
{
theImage.src = "/some/other/image.jpg";
}
// Tell the window to call updateImageWhenHashChanges() again in 500 miliseconds:
window.setTimeout(updateImageWhenHashChanges,500);
}
updateImageWhenHashChanges();
有关更多信息,请阅读 window.setTimeout .
关于c# - 如何根据 #var=test 变量更改图像的图像 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2869560/