c# - 如何根据 #var=test 变量更改图像的图像 url?

标签 c# javascript asp.net jquery

我正在尝试根据 #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/

相关文章:

Javascript `onblur` 事件级联 `Id` 输入元素的 `html`

javascript - 编辑/更新 angularjs 中的项目

c# - Asp.net 核心模型不从表单绑定(bind)

c# - 如何更改双数组中的项目?

c# - 如何通过脚本访问Unity 'Light 2D (Script)'组件?

c# - FabricNotReadableException 是什么意思?我们应该如何应对?

c# - 将 MVC 项目从本地迁移到 Azure - SQL Server 问题

javascript - 在 JavaScript 中使用对象的哈希表功能

c# - 有没有办法为 WebAPI 项目生成 C# HTTPClient 包装器?

asp.net - 在默认应用程序池中关闭回收是否安全