html - 如何通过css在mvc中为 anchor 标签设置图像?

标签 html css asp.net-mvc

我有以下 cshtml 并且工作正常:

var calcButtonStyle = "font-size:smaller;font-weight:normal;position:absolute; width: " + string.Concat(@control.Width / 5, "px") + ";height:" + string.Concat(@control.Height / 5, "px") + ";left:" + string.Concat(@control.LeftLocation + @control.Width + 10, "px") + ";top:" + string.Concat(@control.TopLocation, "px") + " ";
<a href="#" onclick="CalculateExpression('@control.OptionList', '@control.FieldName')" style="@calcButtonStyle"><img src="~/Content/Images/_Summation.svg" /></a>

现在,我想要的是将要从 CSS 呈现的样式(高度、宽度和特别是图像)与其余部分分开。为此,这是我的 css:

#test-compute {
  font-size:smaller;
  font-weight:normal;
  position:absolute;
  height: 20px;
  width: 20px;
  background: url('Images/_Summation.svg');
}

在 cshtml 中,我这样写:

var calcButtonStyle = "left:" + string.Concat(@control.LeftLocation + @control.Width + 10, "px") + ";top:" + string.Concat(@control.TopLocation, "px") + " ";
<a href="#" onclick="CalculateExpression('@control.OptionList', '@control.FieldName')" style="@calcButtonStyle" class="test-compute"></a>

但是图像没有渲染,知道我遗漏了什么地方吗?

提前致谢。

最佳答案

您访问的 DOM 元素不正确,您是通过 id 访问 #test-compute 应该是 .test-compute 才能按类访问。

关于html - 如何通过css在mvc中为 anchor 标签设置图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32503124/

相关文章:

javascript - 无法读取属性 innerHTML Javascript

javascript - 停止特定元素上的传播

html - 无法在 wordpress 中创建垂直广告横幅

html - 无法使用多个媒体查询

javascript - 悬停/焦点状态最初在页面加载时激活

CSS 样式无法识别数字

jquery - 使用 jQuery 的 $.ajax() 将多个 Json 对象作为数据传递

javascript - jQuery .is (":visible") 不工作

c# - 如何在 MVC 中刷新页面

c# - .NET Core MVC Web API Controller 不返回数据,尽管与返回数据的 Controller 相同