【问题标题】:Why ASP.NET Webform not recognizing jQuery Code (CS1056: Unexpected character '$')为什么 ASP.NET Webform 无法识别 jQuery 代码(CS1056:意外字符“$”)
【发布时间】:2021-11-01 07:56:23
【问题描述】:

我对 asp.net 网络表单很陌生,我想在我的母版页中添加一个包含 jquery 代码的 javascript 文件。

这是错误

<body>
<form runat="server">
    <asp:ScriptManager runat="server">
        <Scripts>
            <%--To learn more about bundling scripts in ScriptManager see https://go.microsoft.com/fwlink/?LinkID=301884 --%>
            <%--Framework Scripts--%>
            <asp:ScriptReference Name="MsAjaxBundle" />
            <asp:ScriptReference Name="jquery" />
            <asp:ScriptReference Name="bootstrap" />
            <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
            <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
            <asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
            <asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
            <asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
            <asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
            <asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
            <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
            <asp:ScriptReference Name="WebFormsBundle" />

            <%--Site Scripts--%>
            <script runat="server" src="Scripts/MyScripts/LogicMain.js"></script>

        </Scripts>
    </asp:ScriptManager>

LogicMain.js 是我想要包含在母版页中的文件,因为它会在加载的每个页面上运行。

更新: 我尝试只将 alert() 放在 .js 文件中,但它现在给出了一些其他错误。

【问题讨论】:

  • 语法看起来不正确......也可能在它之前。使用类似: $( "#homeLogo" ).mouseenter(function() { .... (在您的帖子中包含更完整的 JS 代码)
  • 您不能对 JavaScript 文件执行 runat="server"。 Web 窗体中的 JavaScript 在客户端上运行。

标签: asp.net webforms asp.net-webpages


【解决方案1】:

我找到了解决方案,这取决于两件事,

  1. 从脚本中删除 runat="server"。

  2. 在之后添加脚本>

【讨论】:

    【解决方案2】:

    删除这一行

      <script runat="server" src="Scripts/MyScripts/LogicMain.js"></script>
    

    来自脚本 - 放在 &lt;asp:ScriptManager runat="server"&gt; 之后

    【讨论】:

    • 我尝试将脚本放在 之前和之后,但显示相同的错误。
    • 我尝试将它作为正文中的最后一个语句,但仍然没有运气。
    • @HassaanRaza 我不知道你在那里放了什么,或者如果你把它作为 c# 运行 - 你一定不能得到这种 c 尖锐文件的错误......这是一个 javascript 文件, 不能从编译器通过 - 检查为什么通过
    猜你喜欢
    • 2016-11-30
    • 2016-01-08
    • 2010-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-19
    • 1970-01-01
    相关资源
    最近更新 更多