动态渲染 onclick document.location
Dynamically render onclick document.location
我正在使用以下代码:
<tr class="itemList" bgcolor="#A5ACB0" onclick="document.location='Details/'>
但是,我希望 document.location 动态呈现,这样我就可以做这样的事情:
<tr class="itemList" bgcolor="#A5ACB0" onclick="document.location='Details/' + item.ID">
对于那些提问的人,item 是我当前在 for 循环中查看的内容,而 ID 只是该项目的 ID。我正在使用 ASP.NET-MVC,所以这来自该项目的模型。
document.location
是 read only,尽管为其分配一个字符串应该可行。您也可以使用 location.href
.
<tr class="itemList" bgcolor="#A5ACB0" onclick="location.href='Details/' + item.ID">
在您上面的代码中,item.ID
指的是全局范围内可用的 javascript 变量。如果您打算使用 MVC 模型中的值,则需要在其前面放置一个 @ 符号,假设它是一个 int。
<tr class="itemList" bgcolor="#A5ACB0" onclick="location.href='Details/' + @item.ID">
如果是字符串,您需要用引号将其括起来,以便将其解析为 javascript 文字字符串。
<tr class="itemList" bgcolor="#A5ACB0" onclick="location.href='Details/' + '@item.ID'">
我正在使用以下代码:
<tr class="itemList" bgcolor="#A5ACB0" onclick="document.location='Details/'>
但是,我希望 document.location 动态呈现,这样我就可以做这样的事情:
<tr class="itemList" bgcolor="#A5ACB0" onclick="document.location='Details/' + item.ID">
对于那些提问的人,item 是我当前在 for 循环中查看的内容,而 ID 只是该项目的 ID。我正在使用 ASP.NET-MVC,所以这来自该项目的模型。
document.location
是 read only,尽管为其分配一个字符串应该可行。您也可以使用 location.href
.
<tr class="itemList" bgcolor="#A5ACB0" onclick="location.href='Details/' + item.ID">
在您上面的代码中,item.ID
指的是全局范围内可用的 javascript 变量。如果您打算使用 MVC 模型中的值,则需要在其前面放置一个 @ 符号,假设它是一个 int。
<tr class="itemList" bgcolor="#A5ACB0" onclick="location.href='Details/' + @item.ID">
如果是字符串,您需要用引号将其括起来,以便将其解析为 javascript 文字字符串。
<tr class="itemList" bgcolor="#A5ACB0" onclick="location.href='Details/' + '@item.ID'">