如何将ID添加到@ Html.DisplayFor [英] How to add id to @Html.DisplayFor

查看:226
本文介绍了如何将ID添加到@ Html.DisplayFor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将ID添加到

I'm trying to add id to

@Html.DisplayFor(model => model.ScreenWidth, new { @id = "width"})

然后使用

document.getElementById("width").innerHTML = w;

为什么不起作用?

推荐答案

DisplayFor帮助器不会生成任何HTML标记,它只会输出格式化的文本.因此,您不能向其中添加id属性.相反,您可以做的是将其包装在标签(div或span)中:

The DisplayFor helper doesn't generate any HTML tags, it just outputs the formatted text. So you cannot add id attribute to it. What you could do instead is to wrap it in a tag (div or span):

<div id="width">
    @Html.DisplayFor(model => model.ScreenWidth)
</div>

,然后可以操纵此标记的内容:

and then be able to manipulate the contents of this tag:

document.getElementById("width").innerHTML = w;

这篇关于如何将ID添加到@ Html.DisplayFor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆