gpt4 book ai didi

javascript - (new { htmlAtributes = new { }) 和 (new { }) 有什么区别

转载 作者:行者123 更新时间:2023-11-29 18:42:33 30 4
gpt4 key购买 nike

我知道它们是匿名类型,但我不明白 Razor 语法。在一些文档中,我找到了这样的示例:

@Html.Label("Hello", new { htmlAtributes = new { id = "hi"}})

在大多数情况下,我只是使用这个简单的:@Html.Label("Hello", new { id = "hi"})

帮助我理解这一点,我不知道为什么我的 document.getElementById('hi').innerHTML = "changed"; 不适用于第一个。

最佳答案

第一种方式用于Html.LabelFor

@Html.LabelFor(c=>c.Email, new { htmlAtributes = new { id = "hi" } })

如果你使用 @Html.Label("Hello", new { htmlAtributes = new { id = "hi"}})

它会生成到(其中htmlatributes是label标签的一个属性)

<label for="Hello" htmlatributes="{ id = hi }">Hello</label>

你需要使用第二种方式

@Html.Label("Hello", new { id = "hi" })

关于javascript - (new { htmlAtributes = new { }) 和 (new { }) 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55930965/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com