我尝试添加以下javascript代码。

 <script>
    @if (ViewBag.checkedArtikel != null)
    {
        foreach (int ac in ViewBag.checkedArtikel)
        {
            String temp = "'#addartikel" + ac + "'";
            <text> $(@temp).toggleClass('down');</text>
        }
    }
    </script>


如果我省略脚本标签,我会得到正确的jquery命令:

$('#addartikel1').toggleClass('down');


但是用脚本标签我得到这个错误:

Uncaught SyntaxError: Unexpected token &

最佳答案

使用此代码:

<text>$(@Html.Raw(temp)).toggleClass('down');</text>


或者,您可以使用旧代码而无需在变量中添加引号:

String temp = "#addartikel" + ac;
<text> $('@temp').toggleClass('down');</text>

10-01 03:56
查看更多